Basic SQL II Dr Fawaz Alarfaj Al Imam Mohammed Ibn Saud Islamic University ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation
Lab 1 Review Retrieve the birthdate and address of the employee(s) whose last name is king 2
Lab 1 Review SELECT bdate, address FROM employee WHERE lname = 'King' 3
Lab 1 Review Retrieve the first and last name of the employee(s) whose middle initial is A 4
Lab 1 Review SELECT fname, lname FROM employee WHERE minit = A 5
Lab 1 Review Retrieve departments names and first and last names of department's managers 6
Lab 1 Review SELECT dname, fname, lname FROM employee, department WHERE ssn = mgrssn 7
Lab 1 Review Retrieve first and last names of all married employees 8
Lab 1 Review SELECT fname, lname FROM employee, dependent WHERE essn = ssn AND relationship= Spouse' 9
Lab 1 Review Retrieve all departments (name and number) located in Houston 10
Lab 1 Review SELECT dname, department.dnumber FROM department, dept_locations WHERE department.dnumber = dept_locations.dnumber AND dlocation= Houston' 11
Lab 1 Review Retrieve the employee's first and last name for each employee whose immediate supervisor last name is king 12
Lab 1 Review SELECT e2.fname, e2.lname FROM WHERE AND employee as e1, employee as e2 e1.ssn = e2.superssn e1.lname='king' 13
Lab 1 Review List the first name, last name, and ID for each supervisor without duplication 14
Lab 1 Review SELECT fname, lname, ssn FROM WHERE employee, ssn in (SELECT DISTINCT superssn FROM employee) 15
Tables as Sets in SQL Set operations UNION, EXCEPT (difference), INTERSECT Corresponding multiset operations: UNION ALL, EXCEPT ALL, INTERSECT ALL) Type compatibility is needed for these operations to be valid 16
Tables as Sets in SQL 17
Tables as Sets in SQL Example Database ID Name Major 1123 Ali IS 1122 Naif CS 1124 Saad CS 1128 Omar CS Network ID Name Major 1124 Saad CS 1125 Hasan IS 1126 Hamad IS 1122 Naif CS 1127 Majed IS 18
Tables as Sets in SQL UNION operator returns all the unique rows from both the queries Select Id, Name, Major from Database UNION Select Id, Name, Major from Network 19
Tables as Sets in SQL ID Name Major 1124 Saad CS 1125 Hasan IS 1126 Hamad IS 1122 Naif CS 1127 Majed IS 1123 Ali IS 1128 Omar CS 20
Tables as Sets in SQL UNION ALL operator returns all the rows from both the queries, including the duplicates Select Id, Name, Major from Database UNION ALL Select Id, Name, Major from Network 21
Tables as Sets in SQL ID Name Major 1124 Saad CS 1125 Hasan IS 1126 Hamad IS 1122 Naif CS 1127 Majed IS 1123 Ali IS 1122 Naif CS 1124 Saad CS 1128 Omar CS 22
Tables as Sets in SQL INTERSECT operator retrieves the common unique rows from both the left and the right query. Notice the duplicates are removed. Select Id, Name, Major from Database INTERSECT Select Id, Name, Major from Network 23
Tables as Sets in SQL ID Name Major 1122 Naif CS 1124 Saad CS 24
Tables as Sets in SQL EXCEPT operator returns unique rows from the left query that aren t in the right query s results. Select Id, Name, Major from Database EXCEPT Select Id, Name, Major from Network 25
Tables as Sets in SQL ID Name Major 1123 Ali IS 1128 Omar CS 26
Substring Pattern Matching LIKE comparison operator Used for string pattern matching (%) replaces an arbitrary number of zero or more characters underscore (_) replaces a single character Examples: WHERE NAME LIKE H% ; WHERE NAME LIKE _ a ; 27
Substring Pattern Matching Quiz 28
Arithmetic Operators BETWEEN comparison operator SELECT * FROM employee WHERE(Salary BETWEEN 30000 AND 40000 ) ; 29
Arithmetic Operations Standard arithmetic operators: Addition (+), subtraction ( ), multiplication (*), and division (/) may be included as a part of SELECT 30
Arithmetic Operations Show the resulting salaries if every department s manager is given a 10 percent raise. SELECT Fname, Lname, 1.1 * Salary AS Increased_sal FROM WHERE EMPLOYEE, DEPARTMENT Ssn=mgrssn 31
Ordering of Query Results Use ORDER BY clause Keyword DESC to see result in a descending order Keyword ASC to specify ascending order Typically placed at the end of the query ORDER BY D.Dname DESC, E.Lname ASC, E.Fname ASC 32
Ordering of Query Results 33