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

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

관계 연산자 & SQL

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

Projection SELECT A,C FROM r WHERE...

Cartesian Product

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

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

Union SELECT * FROM r UNION SELECT * FROM s

Example

Example - projection

Example - select & project

Example - join

Example - select & project

Example - join

SQL 질의 예제

관계형 스키마 예제  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)

질의 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’

질의 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

질의 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’

질의 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

질의 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’)

질의 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)

질의 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)

질의 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)

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

질의 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

질의 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’

질의 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

질의 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

질의 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

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

질의 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’

질의 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