Structured Query Language Introduction
Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara BillingsDavid DanceIvanna JonesMary BarkerBob WoodsRobin JonesMary
Where Clause SELECT lname, fname, phone FROM employees WHERE dept = ‘ACTG’; Employees Table LNAMEFNAMEDEPTPHONE ITR FINC BillingsDavidACTG DanceIvannaACTG ITR BarkerBobACTG ITR FINC
Virtual Fields SELECT lname, fname, phone, salary/12 FROM employees WHERE dept = ‘ACTG’; Employees Table LNAMEFNAMEDEPTPHONE ITR FINC BillingsDavidACTG DanceIvannaACTG ITR BarkerBobACTG ITR FINC SALARY/
Naming Virtual Fields SELECT lname, fname, phone, salary/12 AS MonthlySal FROM employees WHERE dept = ‘ACTG’; Employees Table LNAMEFNAMEDEPTPHONE ITR FINC BillingsDavidACTG DanceIvannaACTG ITR BarkerBobACTG ITR FINC MonthlySal
Order By Clause SELECT lname, fname, phone, salary/12 as MonthlySal FROM employees WHERE dept = ‘ACTG’ ORDER BY MonthlySal Desc LNAMEFNAMEPHONE BillingsDavid DanceIvanna BarkerBob MonthlySal
Order By Clause (multiple keys) SELECT lname, fname, phone, dept, salary FROM employees ORDER BY Dept ASC, Salary DESC LNAMEFNAMEDEPTPHONESALARY JonesMarkITR SmithSaraFINC BillingsDavidACTG DanceIvannaACTG JonesMaryITR BarkerBobACTG WoodsRobinITR JonesMaryFINC primary sort key secondary sort key
“Like” EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR Sara David Ivanna 32JonesMaryITR Bob Robin 37JonesMaryFINC Employees Table SEX M F F SELECT * FROM employees WHERE fname LIKE ‘M%’;
Combining Conditions Retrieve all Columns for employees working in ITR that have a salary less than
Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR FINC49000 ACTG42000 ACTG60000 ITR70000 ACTG44000 ITR90000 FINC56000 Employees Table SEX M SELECT * FROM employees WHERE dept = ‘ITR’ AND salary < 50000;
Combining Conditions: English is not very structured Retrieve all Columns for all employees that work in the finance and ITR departments.
Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY ITR FINC ACTG ITR ACTG ITR FINC Employees Table SEX SELECT * FROM employees WHERE dept = ‘ITR’ AND dept=‘FINC’;
Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC ACTG 32JonesMaryITR ACTG 36WoodsRobinITR JonesMaryFINC Employees Table SEX M F F M F SELECT * FROM employees WHERE dept = ‘ITR’ OR dept=‘FINC’;
Combining Conditions: Order of Operation Retrieve all Columns for all employees that work in either the finance or ITR departments and that also make less than $50000.
Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC ACTG42000 ACTG JonesMaryITR ACTG WoodsRobinITR Employees Table SEX M F F M SELECT * FROM employees WHERE dept = ‘ITR’ OR dept=‘FINC’ and salary < 50000
Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC ACTG42000 ACTG60000 ITR70000 ACTG44000 ITR90000 FINC56000 Employees Table SEX M F SELECT * FROM employees WHERE (dept = ‘ITR’ OR dept=‘FINC’) and salary < 50000
IN comparator EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC ACTG 32JonesMaryITR ACTG 36WoodsRobinITR JonesMaryFINC Employees Table SEX M F F M F SELECT * FROM employees WHERE dept IN(‘ITR’,’FINC’);
Distinct Key Word EMPIDLNAMEFNAMEDEPTPHONESALARY ITR FINC ACTG ITR ACTG ITR FINC Employees Table SEX SELECT DISTINCT dept FROM employees;
Analytic Functions EMPIDLNAMEFNAMEDEPTPHONESALARY Employees Table SEX SELECT AVG(salary) FROM employees; AVG(SALARY)
SQL Analytic Functions AVG( ) MIN( ) MAX( ) SUM( ) COUNT( ) VAR() STD()
Group Functions EMPIDLNAMEFNAMEDEPTPHONESALARY ITR45000 FINC49000 ACTG42000 ACTG60000 ITR70000 ACTG44000 ITR90000 FINC56000 Employees Table SEX SELECT dept, AVG(salary) FROM employees GROUP BY dept; AVG(SALARY)
Grouping by Multiple Attributes DEPT FINC ACTG ITR SEX F M F F M SELECT dept, sex, AVG(salary) FROM employees GROUP BY dept, sex; AVG(SALARY) FINCM No male finance employees
Having Clause Base Table Inter. Result Set 1 WHERE Inter. Result Set 2 GROUP BY Inter. Result Set 3 HAVING Final Results ORDER BY
Having Clause (example) EMPIDLNAMEFNAMEDEPTPHONESALARY ITR45000 FINC49000 ACTG42000 ACTG60000 ITR70000 ACTG44000 ITR90000 FINC56000 Employees Table SEX SELECT dept, AVG(salary) FROM employees GROUP BY dept HAVING AVG(salary)<50000; AVG(SALARY)
Nested Queries Group functions are likely candidates for Nested Queries. For Example: SELECT lname, fname, phone, dept, salary FROM employees WHERE salary = (Select max(salary) from employees); The above query provides information on the employee with the highest salary. The inner query (shown in blue) will execute first and provide a numeric value which can be used to evaluate the condition on the outer query.
Nested Queries (Example 2) A nested query can also be created by replacing the table name in a WHERE clause with a subquery. For example: SELECT count(dept) FROM (SELECT dept, avg(salary) FROM employees GROUP BY dept HAVING avg(salary) > 40000) The above query provides the number of departments that have an average salary greater than $40,000. The inner query produces a table. The outer query counts the rows In that table.
Retrieving Info from Multiple Tables Programs Table EMPIDLANGUAGE PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC BillingsDavidACTG DanceIvannaACTG JonesMaryITR BarkerBobACTG WoodsRobinITR JonesMaryFINC Employees Table SEX M F M F F M M F VBGOOD VB SQL COBOL JAVA COBOLEXCELLENT GOOD EXCELLENT NAME COBOL SQL JAVA VB FULLNAME COmmon Business Oriented Language Structured Query Language JAVA Visual Basic Languages Table 37SQLFAIR
Key Relational Operators Join (Product) –Inner Join and Natural Inner Join –Outer Join and Natural Outer Join –Cross Join Intersect Minus (Difference) Union Divide (not implemented by most DBMS)
Primary Key Foreign Key Pairs: The Basis for Joins Programs Table EMPIDLANGUAGE PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC BillingsDavidACTG DanceIvannaACTG JonesMaryITR BarkerBobACTG WoodsRobinITR JonesMaryFINC Employees Table SEX M F M F F M M F VBGOOD VB SQL JAVA COBOLEXCELLENT NAME COBOL SQL JAVA VB FULLNAME COmmon Business Oriented Language Structured Query Language JAVA Visual Basic Languages Table 37 COBOL GOOD 37SQLFAIR
Query Illustrating Join Programs Table EMPIDLANGUAGE PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT Employees Table VBGOOD VB SQL JAVA COBOLEXCELLENT SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID= employees.empID EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC BillingsDavidACTG DanceIvannaACTG JonesMaryITR BarkerBobACTG WoodsRobinITR JonesMaryFINC SEX M F M F F M M F 37 COBOL GOOD 37SQLFAIR
Join Syntax Programs Table EMPIDLANGUAGE COBOL SQL JAVA EMPIDLNAMEFNAMEPHONE 23JonesMark DanceIvanna JonesMary WoodsRobin JonesMary Employees Table VB SQL COBOL JAVA COBOL SELECT employees.empid, language, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID= employees.empID 37SQL
Initial result set before projection and selection Programs Table Inner Join Employees Table EMPIDLANGUAGE PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT VBGOOD VB SQL COBOL JAVA COBOLEXCELLENT GOOD EXCELLENT SELECT * FROM programs INNER JOIN employees ON programs.empID= employees.empID EMPIDLNAMEFNAMEDEPTPHONESALARY 31DanceIvannaACTG JonesMaryITR JonesMaryFINC SEX F F F 23JonesMarkITR M 23JonesMarkITR M 32JonesMaryITR F 32JonesMaryITR F 32JonesMaryITR F 36WoodsRobinITR M 36WoodsRobinITR M 36WoodsRobinITR M 23JonesMarkITR M 37SQLFAIR37JonesMaryFINC F
Join Result Set LANGUAGE COBOL SQL JAVA LNAMEFNAMEPHONE JonesMark DanceIvanna JonesMary WoodsRobin JonesMary VB SQL COBOL JAVA COBOL SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID = employees.empID JonesMark JonesMark JonesMary JonesMary JonesMary EMPID WoodsRobin WoodsRobin JonesMary SQL37
Further Selection of Joined Data LANGUAGE SQL LNAMEFNAMEPHONE DanceIvanna JonesMary SQL SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID = employees.empID WHERE Language = ‘SQL’ JonesMark JonesMary EMPID WoodsRobin
OLD Inner Join Syntax LANGUAGE SQL LNAMEFNAMEPHONE DanceIvanna JonesMary SQL SELECT language, employees.empid, lname, fname, phone FROM programs, employees WHERE programs.empID= employees.empID AND Language = ‘SQL’ JonesMark JonesMary EMPID WoodsRobin SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID= employees.empID WHERE Language = ‘SQL’ OLD NEW
Three Table Inner Join Syntax SELECT programs.empid, name, fullname, phone FROM programs, employees, languages WHERE programs.empID= employees.empID AND languages.name = programs.language AND Proficiency = ‘Excellent’ SELECT programs.empid, name, fullname, phone FROM programs INNER JOIN employees ON programs.empID= employees.empID INNER JOIN languages ON languages.name = programs.language WHERE Proficiency = ‘Excellent’ OLD NEW
New Natural Join Syntax (if column names are the same) LANGUAGE SQL LNAMEFNAMEPHONE DanceIvanna JonesMary SQL SELECT language, empid, lname, fname, phone FROM programs, employees WHERE programs.empID= employees.empID AND Language = ‘SQL’ JonesMark JonesMary EMPID WoodsRobin SELECT language, empid, lname, fname, phone FROM programs NATURAL INNER JOIN employees WHERE Language = ‘SQL’ OLD NEW
Outer Join Result Set LANGUAGE COBOL SQL JAVA LNAME FNAMEPHONE JonesMark DanceIvanna JonesMary WoodsRobin JonesMary VB SQL COBOL JAVA COBOL SELECT language, employees.empid, lname, fname, phone from programs RIGHT OUTER JOIN employees ON programs.empID = employees.empID JonesMark JonesMark JonesMary JonesMary JonesMary EMPID WoodsRobin WoodsRobin JonesMary SQL37 25SmithSara BillingsDavid BarkerBob
Outer Join Syntax LNAMEFNAMECOUNT(Language) SmithSara0 JonesMary4 SELECT employees.empID, lname, fname, count(language) FROM programs, employees WHERE programs.empID(+) = employees.empID GROUP BY employees.empID, lname, fname JonesMark3 BillingsDavid0 DanceIvanna1 SELECT employees.empID, lname, fname, count(language) FROM programs RIGHT OUTER JOIN employees ON programs.empID = employees.empID GROUP BY employees.empID, lname, fname OLD NEW (Note: There is new FULL OUTER JOIN also which is a simultaneous LEFT and RIGHT outer JOIN.) JonesMary2 BarkerBob0 WoodsRobin3 EMPID
Cross Join Syntax SELECT lname, fname, languages.name FROM employees, languages; SELECT lname, fname, languages.name FROM employees CROSS JOIN languages; OLD NEW (e.g. JOIN every row in languages table with every row in employees table. 8x4=32 rows returned)
When not to join? If a client program needs data from two tables that does not necessarily mean that a join should occur. Unless the data needs to be viewed as a single table it may be appropriate to simple retrieve to result sets.
Which Employees know both JAVA and COBOL? What is the Query? Programs Table EMPIDLANGUAGE PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR SmithSaraFINC BillingsDavidACTG DanceIvannaACTG JonesMaryITR BarkerBobACTG WoodsRobinITR JonesMaryFINC Employees Table SEX M F M F F M M F VBGOOD VB SQL COBOL JAVA COBOLEXCELLENT GOOD EXCELLENT NAME COBOL SQL JAVA VB FULLNAME COmmon Business Oriented Language Structured Query Language JAVA Visual Basic Languages Table 37SQLFAIR
Intersect Consider: Set R1 { a, b, d, f, h} Set R2 {b, c, f, k, p} Which elements are common between the two sets?
Intersect Consider: Set R1 { a, b, d, f, h} Set R2 {b, c, f, k, p} Which elements are common between the two sets? a b c d f p k h
Intersect Used for Desired Query: Programmers that know JAVA and COBOL SELECT empid FROM programs WHERE language = 'JAVA' INTERSECT SELECT empid FROM programs WHERE language = 'COBOL'; JAVA programmers COBOL programmers
Minus Used for Desired Query: Programmers that know JAVA but not COBOL SELECT empid FROM programs WHERE language = 'JAVA' MINUS SELECT empid FROM programs WHERE language = 'COBOL'; JAVA programmers COBOL programmers
Minus Used for Desired Query: Programmers that know COBOL but not JAVA SELECT empid FROM programs WHERE language = 'COBOL'; MINUS SELECT empid FROM programs WHERE language = 'JAVA' JAVA programmers COBOL programmers
UNION can be used to combine elements (rows) from two sets SELECT lname, fname, phone FROM customers WHERE city = 'Radford'; UNION SELECT lname, fname, phone FROM employees WHERE city = 'Radford' Example: Construct a single list of customer and employee phone numbers for people that live in Radford. Fieldnames do not have to match but the first set of field Names will be used for the result set. The fields must be of the same data type.
Combining Relational Operations SELECT empid, fname, lname, count(language) FROM employees NATURAL LEFT OUTER JOIN programs GROUP BY empid, fname, lname Like any math operations, relational operations can be Combined to handle more complex situations. Example: Give the empid, First Name, Last Name, and number of Programming languages knows for all employees: In this case an OUTER JOIN has been combined with a GROUP BY operation.
Need for CASE statement PROFICIENCY EXCELLENT GOOD EXCELLENT EMPID FAIR SELECT empID, proficiency FROM programs WHERE Language = 'SQL' ORDER BY proficiency;
Use of CASE statement PROFICIENCY EXCELLENT GOOD EXCELLENT EMPID FAIR SELECT empID, proficiency FROM programs WHERE Language = 'SQL' ORDER BY (CASE proficiency WHEN 'EXCELLENT' THEN 1 WHEN 'GOOD' THEN 2 WHEN 'FAIR' THEN 3 END);
Use of old DECODE statement PROFICIENCY EXCELLENT GOOD EXCELLENT EMPID FAIR SELECT empID, proficiency FROM programs WHERE Language = 'SQL' ORDER BY DECODE(proficiency, 'EXCELLENT', 1, 'GOOD',2, 'FAIR',3 );