Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:

Similar presentations


Presentation on theme: "1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:"— Presentation transcript:

1 1 CSE 480: Database Systems Lecture 11: SQL

2 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:

3 3 SELECT Clause SELECT l Attribute list may include –* (wildcard) –Keywords such as AS and DISTINCT –Arithmetic expression –String functions –Boolean expression

4 4 Wildcard(*) in SELECT-Clause l Retrieve the values for all columns of the selected tuples

5 5 AS in SELECT-Clause l Rename the columns in query result l Example: Find the names of employees who earn more than their supervisors SELECT E.FNAME AS EMP_FNAME, E.LNAME AS EMP_LNAME FROM EMPLOYEE E, EMPLOYEE S WHERE E.SUPERSSN=S.SSN AND E.SALARY > S.SALARY; EMP_FNAMEEMP_LNAME Output:

6 6 DISTINCT in SELECT-Clause l Eliminate duplicate tuples in query result

7 7 Arithmetic Expression in SELECT-Clause l Query: Show the effect of giving employees a 10% raise

8 8 String functions in SELECT-Clause

9 9

10 10 String functions in SELECT-Clause For more string functions, go to http://dev.mysql.com/doc/refman/4.1/en/string-functions.html http://dev.mysql.com/doc/refman/4.1/en/string-functions.html

11 1 Boolean Expression in SELECT-Clause

12 12 FROM Clause SELECT FROM l Table list may include –Names of 1 or more tables –Subquery for joined tables

13 13 Joined Relations in FROM-Clause l Query 1: Retrieve the name and address of all employees who work for the 'Research' department SELECT Fname, Lname, Address FROM EMPLOYEE, DEPARTMENT WHERE Dname='Research' AND Dno=Dnumber is equivalent to: SELECT Fname, Lname, Address FROM (EMPLOYEE JOIN DEPARTMENT ON Dno=Dnumber) WHERE Dname='Research'

14 14 Joined Relations in FROM-Clause l Many types of Join –regular JOIN –NATURAL JOIN –CROSS JOIN –LEFT OUTER JOIN –RIGHT OUTER JOIN, –etc

15 15 JOIN Id Name 111 John 222 Mary 333 Bill 444 Joe Id Value 111 B 111 A 222 A R S SELECT * FROM R JOIN S on R.Id = S. Id foreach row r in table R foreach row s in table S if r.Id = s.Id then output the merged row of r and s Id Name Id Value 111 John 111 B 111 John 111 A 222 Mary 222 A

16 16 JOIN – Example Id Name Addr Status 111 John ….. ….. 222 Mary ….. ….. 333 Bill ….. ….. 444 Joe ….. ….. StudId CrsCode Sem Grade 111 CSE305 S00 B 111 CSE306 S99 A 222 CSE304 F99 A StudentTranscript Id Name Addr Status StudId CrsCode Sem Grade 111 John ….. ….. 111 CSE305 S00 B 111 John ….. ….. 111 CSE306 S99 A 222 Mary ….. ….. 222 CSE305 F99 A SELECT * FROM Student JOIN Transcript on Id = StudId Produces columns (attributes) with identical values, which is redundant

17 17 Natural Join l Join condition equates all attributes with the same name l Duplicate columns are automatically eliminated from result Id Name 111 John 222 Mary 333 Bill 444 Joe Id Value 111 B 111 A 222 A R S SELECT * FROM R NATURAL JOIN S foreach row r in table R foreach row s in table S Examine their common attributes If their values are the same, then merge the rows while removing their duplicate columns Id Name Value 111 John B 111 John A 222 Mary A

18 18 Natural Join Id Name Addr Status 111 John ….. ….. 222 Mary ….. ….. 333 Bill ….. ….. 444 Joe ….. ….. Id CrsCode Sem Grade 111 CSE305 S00 B 111 CSE306 S99 A 222 CSE304 F99 A StudentTranscript2 Id Name Addr Status CrsCode Sem Grade 111 John ….. ….. CSE305 S00 B 111 John ….. ….. CSE306 S99 A 222 Mary ….. ….. CSE305 F99 A SELECT * FROM Student NATURAL JOIN Transcript2 Duplicate attribute (Id) was removed Join attribute must have the same name (Id)

19 19 Cross Join Id Name 111 John 222 Mary 333 Bill 444 Joe Id Value 111 B 111 A 222 A R S SELECT * FROM R CROSS JOIN S foreach row r in table R foreach row s in table S output the merged row r and s Id Name Id Value 111 John 111 B 111 John 111 A 111 John 222 A … … 444 Joe 222 A Output has 12 rows

20 20 Left Outer Join Id Name 111 John 222 Mary 333 Bill 444 Joe Id Value 111 B 111 A 222 A R S SELECT * FROM R LEFT OUTER JOIN S ON R.Id = S.Id ON R.Id = S.Id foreach row r in table R foreach row s in table S. if r.Id = S.Id then output the merged row r and s if row r is not merged with any rows in S output row r with NULL values for s Id Name Id Value 111 John 111 B 111 John 111 A 222 Mary 222 A 333 Bill NULL NULL 444 Joe NULL NULL Output has 5 rows

21 21 Left Outer Join - Example l Retrieve the name of all employees and their supervisors SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S ON E.SUPERSSN=S.SSN)

22 2 Right Outer Join Id Name 111 John 222 Mary 333 Bill 444 Joe Id Value 111 B 111 A 555 A R S SELECT * FROM R RIGHT OUTER JOIN S ON R.Id = S.Id ON R.Id = S.Id foreach row s in table S foreach row r in table R if r.Id = S.Id then output the merged row r and s if row s is not merged with any rows in R output row s with NULL values for r Id Name Id Value 111 John 111 B 111 John 111 A NULL NULL 555 A Output has 3 rows

23 23 WHERE l Selection condition is a Boolean expression –Simple selection condition:  operator  operator or operator –Complex conditions:  AND  OR  NOT WHERE-Clause

24 24  operator –Operator: =, >, =, (not equal to) –Applicable to integers, floats, strings, dates, etc. (except for NULL) SELECT * FROM EMPLOYEE WHERE SSN > SUPERSSN AND SALARY > 49999.99 AND MINIT='B' AND LNAME='SMITH' AND BDATE >= '1980-01-01'; Boolean Expression in WHERE-Clause

25 25 Substring Comparison in WHERE-Clause l Find employees who live in “Houston, TX”. l Use the LIKE operator to compare partial strings l Two reserved characters are used: –% matches an arbitrary number of characters –_ matches a single arbitrary character

26 26 Substring Comparison l Query: Retrieve all employees whose address is in Houston, Texas. SELECT FNAME, LNAME FROMEMPLOYEE WHEREADDRESS LIKE '%Houston, TX% ‘ ;

27 27 Substring Comparison l Query: Retrieve all employees who were born during the 1950s. Q26:SELECT FNAME, LNAME FROMEMPLOYEE WHEREBDATE LIKE '195_ - _ _ - _ _ ‘ ;

28 28 Arithmetic Expression in WHERE-Clause l Query: Retrieve the names of employees who earn more than half the salary of their supervisors SELECT E.FName, E.Lname FROMEMPLOYEE AS E, EMPLOYEE AS S WHEREE.Salary > S.Salary/2 AND E.SuperSSN = S.SSN;

29 29 Arithmetic Expression in WHERE-Clause l Between comparison operator l Query: Retrieve the first and last names of employees in department 5 whose salary is between $30,000 and $40,000. SELECT Fname, Lname FROMEMPLOYEE WHERE(Salary BETWEEN 30000 AND 40000) AND Dno = 5

30 30 UNSPECIFIED WHERE-clause l If there is only one relation in the FROM-clause and there is no join condition, this implies all tuples of the relation are selected SELECT SSN FROMEMPLOYEE; l If more than one relation is specified in the FROM-clause and there is no join condition, then the CARTESIAN PRODUCT (Cross Join) of tuples is selected SELECTSSN, DNAME FROMEMPLOYEE, DEPARTMENT;

31 31 NULLS IN SQL QUERIES l Cannot use equality (=) comparison to check for null values l Query: Retrieve the names of employees who do not have supervisors l Query: Retrieve the names of employees who have supervisors SELECT FNAME, LNAME FROM EMPLOYEE WHERE SUPERSSN IS NULL; SELECT FNAME, LNAME FROM EMPLOYEE WHERE SUPERSSN IS NOT NULL;

32 32 SET OPERATIONS l SQL has directly incorporated some set operations –UNION –INTERSECT –MINUS or EXCEPT l The resulting relations of these set operations are sets of tuples; duplicate tuples are eliminated from the result l The set operations are applicable to union compatible relations –the two SQL relations must have the same attributes and the attributes must appear in the same order l Note: MySQL supports only UNION; Oracle supports all 3 set operations

33 3 Example in Oracle Duplicate rows are eliminated Not available in MySQL

34 34 Example l Query: List the names of projects that involve an employee whose last name is 'Smith' as a worker or as a manager of the department that controls the project

35 35 Example l Query: List the names of 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 PNAME FROMPROJECT, DEPARTMENT, EMPLOYEE WHEREDNUM=DNUMBER AND MGRSSN=SSN AND LNAME='Smith') UNION (SELECT PNAME FROMPROJECT, WORKS_ON, EMPLOYEE WHEREPNUMBER=PNO AND ESSN=SSN AND NAME='Smith');

36 36 Example l Query: List the first name and last name of employees who do not work on any project

37 37 Example l Query: List the first name and last name of employees who do not work on any project (SELECT Fname, Lname FROMEMPLOYEE) MINUS (SELECT Fname, Lname FROMWORKS_ON, EMPLOYEE WHEREESSN=SSN); Caution: Not applicable in MySQL (see slide 40 on how to write this query in MySQL)

38 38 IN Operator v IN W l The comparison operator IN compares a value v with a set of values W, and evaluates to TRUE if v is one of the elements in W. This is SET membership test. l Examples: –3 in {1, 2, 3} TRUE –0 in {1, 2, 3} FALSE

39 39 IN Operator l Query: Retrieve the social security numbers of all employees who work on project number 1, 2, or 3 SELECT DISTINCT ESSN FROMWORKS_ON WHERE(PNO = 1) OR (PNO = 2) OR (PNO = 3); Using IN Operator: SELECT DISTINCT ESSN FROMWORKS_ON WHEREPNO IN (1, 2, 3);

40 40 Example l Query: List the first name and last name of employees who do not work on any project SELECT Fname, Lname FROMEMPLOYEE WHERESSN NOT IN (SELECT ESSN FROM WORKS_ON);

41 41 Exercise l Find the names of supervisors who are also managers l Find the names of employees who are not supervisors l Find the names of supervisors who supervise exactly one employee l Try to write it in two ways: –Using IN operator –Using SET operations (UNION, INTERSECT, MINUS)


Download ppt "1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:"

Similar presentations


Ads by Google