Presentation is loading. Please wait.

Presentation is loading. Please wait.

관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5.

Similar presentations


Presentation on theme: "관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5."— Presentation transcript:

1 관계 연산자 & SQL

2 Selection SELECT * FROM r WHERE A=B AND D>5

3 Projection SELECT A,C FROM r WHERE...

4 Cartesian Product

5 Join - equi, natural join SELECT * FROM r, s WHERE r.B=s.B AND r.D=s.D

6 Join - Theta join SELECT * FROM r, s WHERE r.B<s.D

7 Union SELECT * FROM r UNION SELECT * FROM s

8 Example

9 Example - projection

10 Example - select & project

11 Example - join

12 Example - select & project

13 Example - join

14 SQL 질의 예제

15 관계형 스키마 예제  Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate)  dept_locations (dnumber, dlocation)  Project (pname, pnumber, plocation, dnum)  works_on (essn, pno, hours)  Dependent (essn, dependent_name, sex, bdate, relationship)

16 질의 1 - 기본  Retrieve the birthdate and address of the employee whose name is ‘John B.Smith’ SELECT bdate, address FROM employee WHERE fname = ‘John’ AND minit=‘B’ AND lname=‘Smith’

17 질의 2 - 기본  Retrive the name and address of all employees who work for the ‘Research’ department SELECT fname, lname, address FROM employee, department WHERE dname=‘Research’ AND dnumber=dno

18 질의 3 - 기본  For every project located in ‘Stafford’ list the project number, the controlling department number, and the department manager’s last name, address, and birthdate SELECT pnumber, dnum, lname, address, bdate FROM project, department, employee WHERE dnum-dnumber AND mgrssn=ssn AND plocation=‘Staffod’

19 질의 4 - alisas 의 사용  For each employee, retrieve the employee’s first and last name and the first and last name of his or her immediate supervisor SELECT e.fname, e.lname, s.fname, s.lname FROM employee e s WHERE e.superssn=s.ssn

20 질의 5 - Tables as Sets  Make a list of all project numbers for projects that involve an employee whose last name is ‘Smith’ as a worker or as a manager of the department that controls the project (SELECT pnumber FROM project, department, employee WHERE dnum=dnumber AND mgrssn=ssn AND lname=‘Smith’) UNION (SELECT pnumber FROM project, works_on, employee WHERE pnumber=pno AND essn=ssn AND lname=‘Smith’)

21 질의 6 - nested query  Retrieve the name of each employee who has a dependent with the same first name and same sex as the employee SELECT e.fname, e.lname FROM employee e WHERE e.ssn IN (SELECT essn FROM dependent FROM dependent WHERE essn=e.ssn AND WHERE essn=e.ssn AND e.fname=dependent_name AND e.fname=dependent_name AND sex=e.sex) sex=e.sex)

22 질의 7 - EXIST function  Retrieve the names of employees who have no dependents SELECT fname, lname FROM employee WHERE NOT EXISTS (SELECT * FROM dependent FROM dependent WHERE ssn=essn) WHERE ssn=essn)

23 질의 8 - 집합 명시  Retrieve the social security number of all employees who work on project number 1,2, or 3 SELECT DISTINCT essn FROM works_on WHERE pno IN (1,2,3)

24 질의 9 - NULL 의 사용  Retrieve the names of all employees who do not have supervisors SELECT fname, lname FROM employee WHERE superssn IS NULL

25 질의 10 - 집계함수의 사용 (1)  Find the sum of the salaries of all employees, the maximum salary, the minimum salary, and the average salary SELECT SUM(salary), MAX(salary), MIN(salary) FROM employee

26 질의 11 - 집계함수의 사용 (2)  Retrieve the total number of employees in the company and the number of employees in the ‘Research’ department  SELECT COUNT(*) FROM employee  SELECT COUNT(*) FROM employee, department FROM employee, department WHERE dno=dnumber AND dname=‘Research’ WHERE dno=dnumber AND dname=‘Research’

27 질의 12 - 집계함수의 사용 (3)  For each department, retrieve the department number,the number of employees in the department, and their average salary SELECT dno, COUNT(*), AVG(salary) FROM employee GROUP BY dno

28 질의 13 - 집계함수의 사용 (4)  For each project on which more than two employees work retrieve the project number, project name, and number of employees who work on that project SELECT pnumber, pname, COUNT(*) FROM project, works_on WHERE pnumber=pno GROUP BY pnumber, pname HAVING COUNT(*) >2

29 질의 14 - 집계함수의 사용 (5)  For each project, retrieve the project number, project name, and number of employees from department 5 who work on that project SELECT pnumber, pname, COUNT(*) FROM project, works_on, employee WHERE pnumber=pno AND ssn=essn AND dno=5 GROUP BY pnumber, pname

30 질의 15 - 문자열 검색  Retrieve all employees who were born during the 1950s SELECT fname, lname FROM employee WHERE bdate LIKE ‘______5_’

31 질의 16 - 산술 연산  Find the salary lists to give all employees who work on the ‘Product X’ project a 10% raise SELECT fname, lname, 1,1*salary FROM employee, works_on, project WHERE ssn=essn AND pno=pnumber AND pname=‘ProductX’

32 질의 15 - 정렬  List ordered by the employee’s department and within each department ordered alphabetically by name SELECT dname, lname, fname, pname FROM department, employee, works_on, project WHERE dnumber=dno AND ssn=essn AND pno=pnumber ORDERED BY dname, lname, fname


Download ppt "관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5."

Similar presentations


Ads by Google