관계 연산자 & sql. selection select * from r where a=b and d>5

32
관관 관관관 관관 관관관 & & SQL SQL

Post on 22-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

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

관계 연산자 관계 연산자 &&SQLSQL

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

SelectionSelection

SELECT *FROM rWHERE A=B AND D>5

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

ProjectionProjection

SELECT A,CFROM rWHERE ...

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

Cartesian ProductCartesian Product

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

Join - equi, natural joinJoin - equi, natural join

SELECT *FROM r, sWHERE r.B=s.B AND r.D=s.D

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

Join - Theta joinJoin - Theta join

SELECT *FROM r, sWHERE r.B<s.D

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

UnionUnion

SELECT *FROM rUNIONSELECT *FROM s

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

ExampleExample

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

Example Example - projection- projection

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

Example Example - select & project- select & project

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

Example Example - join- join

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

Example Example - select & project- select & project

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

Example Example - join- join

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

SQL SQL 질의 예제질의 예제

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

관계형 스키마 예제관계형 스키마 예제 EmployeeEmployee (fname, minit, lname, (fname, minit, lname, ssnssn, bdate, address, sex, sala, bdate, address, sex, sala

ry, superssn, dno) ry, superssn, dno)

DepartmentDepartment (dname, (dname, dnumberdnumber, mgrssn, mgrstartdate) , mgrssn, mgrstartdate)

dept_locationsdept_locations ( (dnumberdnumber, , dlocationdlocation) )

ProjectProject (pname, (pname, pnumberpnumber, plocation, dnum), plocation, dnum)

works_onworks_on ( (essnessn, , pnopno, hours), hours)

DependentDependent ( (essnessn, , dependent_namedependent_name, sex, bdate, relationship), sex, bdate, relationship)

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

질의질의 1 1 - - 기본기본

Retrieve the birthdate and address of the eRetrieve the birthdate and address of the employee whose name is ‘John B.Smith’mployee whose name is ‘John B.Smith’

SELECT bdate, addressSELECT bdate, addressFROM employeeFROM employeeWHERE fname = ‘John’ AND minit=‘B’ AND lname=WHERE fname = ‘John’ AND minit=‘B’ AND lname=

‘Smith’‘Smith’

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

질의질의 2 2 - - 기본기본

Retrive the name and address of all emploRetrive the name and address of all employees who work for the ‘Research’ departmeyees who work for the ‘Research’ departmentnt

SELECT fname, lname, addressSELECT fname, lname, addressFROM employee, departmentFROM employee, departmentWHERE dname=‘Research’ AND dnumber=dnoWHERE dname=‘Research’ AND dnumber=dno

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

질의 질의 3 3 - - 기본기본 For every project located in ‘Stafford’ list the pFor every project located in ‘Stafford’ list the p

roject number, the controlling department numroject number, the controlling department number, and the department manager’s last namber, and the department manager’s last name, address, and birthdatee, address, and birthdate

SELECT pnumber, dnum, lname, address, bdateSELECT pnumber, dnum, lname, address, bdateFROM project, department, employeeFROM project, department, employeeWHERE dnum-dnumber AND mgrssn=ssn AND plocation=‘StafWHERE dnum-dnumber AND mgrssn=ssn AND plocation=‘Staf

fod’fod’

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

질의 질의 4 4 - alisas- alisas 의 사용의 사용

For each employee, retrieve the employee’s first For each employee, retrieve the employee’s first and last name and the first and last name of his and last name and the first and last name of his or her immediate supervisoror her immediate supervisor

SELECT e.fname, e.lname, s.fname, s.lnameSELECT e.fname, e.lname, s.fname, s.lnameFROM employee e sFROM employee e sWHERE e.superssn=s.ssnWHERE e.superssn=s.ssn

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

질의 질의 5 5 - Tables as Sets- Tables as Sets Make a list of all project numbers for projects that involve aMake a list of all project numbers for projects that involve a

n employee whose last name is ‘Smith’ as a worker or as a n employee whose last name is ‘Smith’ as a worker or as a manager of the department that controls the projectmanager of the department that controls the project

(SELECT pnumber(SELECT pnumberFROM project, department, employeeFROM project, department, employeeWHERE dnum=dnumber AND mgrssn=ssn AND lname=‘SmitWHERE dnum=dnumber AND mgrssn=ssn AND lname=‘Smit

h’)h’)UNIONUNION(SELECT pnumber(SELECT pnumberFROM project, works_on, employeeFROM project, works_on, employeeWHERE pnumber=pno AND essn=ssn AND lname=‘Smith’)WHERE pnumber=pno AND essn=ssn AND lname=‘Smith’)

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

질의 질의 6 6 - nested query- nested query

Retrieve the name of each employee who has Retrieve the name of each employee who has a dependent with the same first name and saa dependent with the same first name and same sex as the employeeme sex as the employee

SELECT e.fname, e.lnameSELECT e.fname, e.lnameFROM employee eFROM employee eWHERE e.ssn IN (SELECT essnWHERE e.ssn IN (SELECT essn

FROM dependentFROM dependent WHERE essn=e.ssn ANDWHERE essn=e.ssn AND

e.fname=dependent_name ANDe.fname=dependent_name AND sex=e.sex)sex=e.sex)

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

질의 질의 7 7 - EXIST function- EXIST function

Retrieve the names of employees who havRetrieve the names of employees who have no dependentse no dependents

SELECT fname, lnameSELECT fname, lnameFROM employeeFROM employeeWHERE NOT EXISTS (SELECT *WHERE NOT EXISTS (SELECT *

FROM dependentFROM dependent WHERE ssn=essn)WHERE ssn=essn)

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

질의 질의 8 8 - - 집합 명시집합 명시

Retrieve the social security number of all eRetrieve the social security number of all employees who work on project number 1,2, mployees who work on project number 1,2, or 3or 3

SELECT DISTINCT essnSELECT DISTINCT essnFROM works_onFROM works_onWHERE pno IN (1,2,3)WHERE pno IN (1,2,3)

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

질의 질의 9 9 - NULL- NULL 의 사용의 사용

Retrieve the names of all employees who dRetrieve the names of all employees who do not have supervisorso not have supervisors

SELECT fname, lnameSELECT fname, lnameFROM employeeFROM employeeWHERE superssn IS NULLWHERE superssn IS NULL

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

질의 질의 10 10 - - 집계함수의 사용집계함수의 사용 (1)(1)

Find the sum of the salaries of all Find the sum of the salaries of all employees, the maximum salary, the employees, the maximum salary, the minimum salary, and the average minimum salary, and the average salarysalary

SELECT SUM(salary), MAX(salary), MIN(salary)SELECT SUM(salary), MAX(salary), MIN(salary)FROM employeeFROM employee

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

질의 질의 11 11 - - 집계함수의 사용집계함수의 사용 (2)(2)

Retrieve the total number of employees in the Retrieve the total number of employees in the company and the number of employees in the company and the number of employees in the ‘Research’ department‘Research’ department

SELECT COUNT(*) FROM employeeSELECT COUNT(*) FROM employee

SELECT COUNT(*)SELECT COUNT(*) FROM employee, departmentFROM employee, department WHERE dno=dnumber AND dname=‘Research’WHERE dno=dnumber AND dname=‘Research’

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

질의질의 12 12 - - 집계함수의 사용집계함수의 사용 (3)(3)

For each department, retrieve the departmFor each department, retrieve the department number ,the number of employees in tent number ,the number of employees in the department, and their average salaryhe department, and their average salary

SELECT dno, COUNT(*), AVG(salary)SELECT dno, COUNT(*), AVG(salary)FROM employeeFROM employeeGROUP BY dnoGROUP BY dno

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

질의질의 13 13 - - 집계함수의 사용집계함수의 사용 (4)(4)

For each project on which more than two employeFor each project on which more than two employees work retrieve the project number, project names work retrieve the project number, project name, and number of employees who work on that pre, and number of employees who work on that projectoject

SELECT pnumber, pname, COUNT(*)SELECT pnumber, pname, COUNT(*)FROM project, works_onFROM project, works_onWHERE pnumber=pnoWHERE pnumber=pnoGROUP BY pnumber, pnameGROUP BY pnumber, pnameHAVING COUNT(*) >2HAVING COUNT(*) >2

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

질의 질의 14 14 - - 집계함수의 사용집계함수의 사용 (5)(5)

For each project, retrieve the project numbFor each project, retrieve the project number, project name, and number of employeeer, project name, and number of employees from department 5 who work on that projs from department 5 who work on that projectect

SELECT pnumber, pname, COUNT(*)SELECT pnumber, pname, COUNT(*)FROM project, works_on, employeeFROM project, works_on, employeeWHERE pnumber=pno AND ssn=essn AND dno=5WHERE pnumber=pno AND ssn=essn AND dno=5GROUP BY pnumber, pnameGROUP BY pnumber, pname

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

질의질의 15 15 - - 문자열 검색문자열 검색

Retrieve all employees who were born duriRetrieve all employees who were born during the 1950sng the 1950s

SELECT fname, lnameSELECT fname, lnameFROM employeeFROM employeeWHERE bdate LIKE ‘______5_’WHERE bdate LIKE ‘______5_’

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

질의질의 16 16 - - 산술 연산산술 연산

Find the salary lists to give all employees Find the salary lists to give all employees who work on the ‘Product X’ project a 10% rwho work on the ‘Product X’ project a 10% raiseaise

SELECT fname, lname, 1,1*salarySELECT fname, lname, 1,1*salaryFROM employee, works_on, projectFROM employee, works_on, projectWHERE ssn=essn AND pno=pnumber AND pname=‘ProductX’WHERE ssn=essn AND pno=pnumber AND pname=‘ProductX’

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

질의질의 15 15 - - 정렬정렬

List ordered by the employee’s departmenList ordered by the employee’s department and within each department ordered alpt and within each department ordered alphabetically by namehabetically by name

SELECT dname, lname, fname, pnameSELECT dname, lname, fname, pnameFROM department, employee, works_on, projectFROM department, employee, works_on, projectWHERE dnumber=dno AND ssn=essn AND pno=pnumberWHERE dnumber=dno AND ssn=essn AND pno=pnumberORDERED BY dname, lname, fnameORDERED BY dname, lname, fname