Download presentation
Presentation is loading. Please wait.
Published byArleen Snow Modified over 9 years ago
1
Structured Query Language Introduction
2
Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark555-1087 SmithSara555-2222 BillingsDavid555-4356 DanceIvanna444-4887 JonesMary555-8745 BarkerBob555-6565 WoodsRobin555-9812 JonesMary555-1234
3
Where Clause SELECT lname, fname, phone FROM employees WHERE dept = ‘ACTG’; Employees Table LNAMEFNAMEDEPTPHONE ITR FINC BillingsDavidACTG555-4356 DanceIvannaACTG444-4887 ITR BarkerBobACTG555-6565 ITR FINC555-1234
4
Virtual Fields SELECT lname, fname, phone, salary/12 FROM employees WHERE dept = ‘ACTG’; Employees Table LNAMEFNAMEDEPTPHONE ITR FINC BillingsDavidACTG555-4356 DanceIvannaACTG444-4887 ITR BarkerBobACTG555-6565 ITR FINC555-1234 SALARY/12 3500 5000 3666
5
Naming Virtual Fields SELECT lname, fname, phone, salary/12 AS MonthlySal FROM employees WHERE dept = ‘ACTG’; Employees Table LNAMEFNAMEDEPTPHONE ITR FINC BillingsDavidACTG555-4356 DanceIvannaACTG444-4887 ITR BarkerBobACTG555-6565 ITR FINC555-1234 MonthlySal 3500 5000 3666
6
Order By Clause SELECT lname, fname, phone, salary/12 as MonthlySal FROM employees WHERE dept = ‘ACTG’ ORDER BY MonthlySal Desc LNAMEFNAMEPHONE BillingsDavid555-4356 DanceIvanna444-4887 BarkerBob555-6565 MonthlySal 3500 5000 3666
7
Order By Clause (multiple keys) SELECT lname, fname, phone, dept, salary FROM employees ORDER BY Dept ASC, Salary DESC LNAMEFNAMEDEPTPHONESALARY JonesMarkITR555-108745000 SmithSaraFINC555-222249000 BillingsDavidACTG555-435642000 DanceIvannaACTG444-488760000 JonesMaryITR555-874570000 BarkerBobACTG555-656544000 WoodsRobinITR555-981290000 JonesMaryFINC555-123456000 primary sort key secondary sort key
8
“Like” EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR555-108745000 Sara David Ivanna 32JonesMaryITR555-874570000 Bob Robin 37JonesMaryFINC555-123456000 Employees Table SEX M F F SELECT * FROM employees WHERE fname LIKE ‘M%’;
9
Combining Conditions Retrieve all Columns for employees working in ITR that have a salary less than 50000.
10
Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR555-108745000 FINC49000 ACTG42000 ACTG60000 ITR70000 ACTG44000 ITR90000 FINC56000 Employees Table SEX M SELECT * FROM employees WHERE dept = ‘ITR’ AND salary < 50000;
11
Combining Conditions: English is not very structured Retrieve all Columns for all employees that work in the finance and ITR departments.
12
Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY ITR FINC ACTG ITR ACTG ITR FINC Employees Table SEX SELECT * FROM employees WHERE dept = ‘ITR’ AND dept=‘FINC’;
13
Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR555-108745000 25SmithSaraFINC555-222249000 ACTG 32JonesMaryITR555-874570000 ACTG 36WoodsRobinITR555-981290000 37JonesMaryFINC555-123456000 Employees Table SEX M F F M F SELECT * FROM employees WHERE dept = ‘ITR’ OR dept=‘FINC’;
14
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.
15
Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR555-108745000 25SmithSaraFINC555-222249000 ACTG42000 ACTG60000 32JonesMaryITR555-874570000 ACTG44000 36WoodsRobinITR555-981290000 56000 Employees Table SEX M F F M SELECT * FROM employees WHERE dept = ‘ITR’ OR dept=‘FINC’ and salary < 50000
16
Combining Conditions EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR555-108745000 25SmithSaraFINC555-222249000 ACTG42000 ACTG60000 ITR70000 ACTG44000 ITR90000 FINC56000 Employees Table SEX M F SELECT * FROM employees WHERE (dept = ‘ITR’ OR dept=‘FINC’) and salary < 50000
17
IN comparator EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR555-108745000 25SmithSaraFINC555-222249000 ACTG 32JonesMaryITR555-874570000 ACTG 36WoodsRobinITR555-981290000 37JonesMaryFINC555-123456000 Employees Table SEX M F F M F SELECT * FROM employees WHERE dept IN(‘ITR’,’FINC’);
18
Distinct Key Word EMPIDLNAMEFNAMEDEPTPHONESALARY ITR FINC ACTG ITR ACTG ITR FINC Employees Table SEX SELECT DISTINCT dept FROM employees;
19
Analytic Functions EMPIDLNAMEFNAMEDEPTPHONESALARY 45000 49000 42000 60000 70000 44000 90000 56000 Employees Table SEX SELECT AVG(salary) FROM employees; 57000 AVG(SALARY)
20
SQL Analytic Functions AVG( ) MIN( ) MAX( ) SUM( ) COUNT( ) VAR() STD()
21
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) 48666 52500 68333
22
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) 60000 52500 67500 43000 70000 FINCM No male finance employees
23
Having Clause Base Table Inter. Result Set 1 WHERE Inter. Result Set 2 GROUP BY Inter. Result Set 3 HAVING Final Results ORDER BY
24
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) 48666 52500 68333
25
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.
26
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.
27
Retrieving Info from Multiple Tables Programs Table EMPIDLANGUAGE 23 32 23 31 32 PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR555-108745000 25SmithSaraFINC555-222255000 26BillingsDavidACTG555-435642000 31DanceIvannaACTG444-488760000 32JonesMaryITR555-874570000 35BarkerBobACTG555-656544000 36WoodsRobinITR555-981290000 37JonesMaryFINC555-123456000 Employees Table SEX M F M F F M M F 32 37 36 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
28
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)
29
Primary Key Foreign Key Pairs: The Basis for Joins Programs Table EMPIDLANGUAGE 23 32 23 31 32 PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR555-108745000 25SmithSaraFINC555-222255000 26BillingsDavidACTG555-435642000 31DanceIvannaACTG444-488760000 32JonesMaryITR555-874570000 35BarkerBobACTG555-656544000 36WoodsRobinITR555-981290000 37JonesMaryFINC555-123456000 Employees Table SEX M F M F F M M F 32 36 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
30
Query Illustrating Join Programs Table EMPIDLANGUAGE 23 32 23 31 32 PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT Employees Table 32 36 VBGOOD VB SQL JAVA COBOLEXCELLENT SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID= employees.empID EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR555-108745000 25SmithSaraFINC555-222255000 26BillingsDavidACTG555-435642000 31DanceIvannaACTG444-488760000 32JonesMaryITR555-874570000 35BarkerBobACTG555-656544000 36WoodsRobinITR555-981290000 37JonesMaryFINC555-123456000 SEX M F M F F M M F 37 COBOL GOOD 37SQLFAIR
31
Join Syntax Programs Table EMPIDLANGUAGE 23 32 23 31 32 COBOL SQL JAVA EMPIDLNAMEFNAMEPHONE 23JonesMark555-1087 25 26 31DanceIvanna444-4887 32JonesMary555-8745 35 36WoodsRobin555-9812 37JonesMary555-1234 Employees Table 32 37 36 VB SQL COBOL JAVA COBOL SELECT employees.empid, language, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID= employees.empID 37SQL
32
Initial result set before projection and selection Programs Table Inner Join Employees Table EMPIDLANGUAGE 23 32 23 31 32 PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT 32 37 36 VBGOOD VB SQL COBOL JAVA COBOLEXCELLENT GOOD EXCELLENT SELECT * FROM programs INNER JOIN employees ON programs.empID= employees.empID EMPIDLNAMEFNAMEDEPTPHONESALARY 31DanceIvannaACTG444-488760000 32JonesMaryITR555-874570000 37JonesMaryFINC555-123456000 SEX F F F 23JonesMarkITR555-108745000M 23JonesMarkITR555-108745000M 32JonesMaryITR555-874570000F 32JonesMaryITR555-874570000F 32JonesMaryITR555-874570000F 36WoodsRobinITR555-981290000M 36WoodsRobinITR555-981290000M 36WoodsRobinITR555-981290000M 23JonesMarkITR555-108745000M 37SQLFAIR37JonesMaryFINC555-123456000F
33
Join Result Set LANGUAGE COBOL SQL JAVA LNAMEFNAMEPHONE JonesMark555-1087 DanceIvanna444-4887 JonesMary555-8745 WoodsRobin555-9812 JonesMary555-1234 VB SQL COBOL JAVA COBOL SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID = employees.empID JonesMark555-1087 JonesMark555-1087 JonesMary555-8745 JonesMary555-8745 JonesMary555-8745 23 31 32 36 37 EMPID WoodsRobin555-9812 WoodsRobin555-9812 36 23 32 JonesMary555-1234SQL37
34
Further Selection of Joined Data LANGUAGE SQL LNAMEFNAMEPHONE DanceIvanna444-4887 JonesMary555-1234 SQL SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID = employees.empID WHERE Language = ‘SQL’ JonesMark555-1087 JonesMary555-8745 31 37 EMPID WoodsRobin555-981236 23 32
35
OLD Inner Join Syntax LANGUAGE SQL LNAMEFNAMEPHONE DanceIvanna444-4887 JonesMary555-1234 SQL SELECT language, employees.empid, lname, fname, phone FROM programs, employees WHERE programs.empID= employees.empID AND Language = ‘SQL’ JonesMark555-1087 JonesMary555-8745 31 37 EMPID WoodsRobin555-981236 23 32 SELECT language, employees.empid, lname, fname, phone FROM programs INNER JOIN employees ON programs.empID= employees.empID WHERE Language = ‘SQL’ OLD NEW
36
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
37
New Natural Join Syntax (if column names are the same) LANGUAGE SQL LNAMEFNAMEPHONE DanceIvanna444-4887 JonesMary555-1234 SQL SELECT language, empid, lname, fname, phone FROM programs, employees WHERE programs.empID= employees.empID AND Language = ‘SQL’ JonesMark555-1087 JonesMary555-8745 31 37 EMPID WoodsRobin555-981236 23 32 SELECT language, empid, lname, fname, phone FROM programs NATURAL INNER JOIN employees WHERE Language = ‘SQL’ OLD NEW
38
Outer Join Result Set LANGUAGE COBOL SQL JAVA LNAME FNAMEPHONE JonesMark555-1087 DanceIvanna444-4887 JonesMary555-8745 WoodsRobin555-9812 JonesMary555-1234 VB SQL COBOL JAVA COBOL SELECT language, employees.empid, lname, fname, phone from programs RIGHT OUTER JOIN employees ON programs.empID = employees.empID JonesMark555-1087 JonesMark555-1087 JonesMary555-8745 JonesMary555-8745 JonesMary555-8745 23 31 32 36 37 EMPID WoodsRobin555-9812 WoodsRobin555-9812 36 23 32 JonesMary555-1234SQL37 25SmithSara555-2222 26BillingsDavid555-4356 35BarkerBob555-6565
39
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 23 25 26 31 32 35 36 37
40
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)
41
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.
42
Which Employees know both JAVA and COBOL? What is the Query? Programs Table EMPIDLANGUAGE 23 32 23 31 32 PROFICIENCY COBOLGOOD SQL JAVA GOOD EXCELLENT EMPIDLNAMEFNAMEDEPTPHONESALARY 23JonesMarkITR555-108745000 25SmithSaraFINC555-222255000 26BillingsDavidACTG555-435642000 31DanceIvannaACTG444-488760000 32JonesMaryITR555-874570000 35BarkerBobACTG555-656544000 36WoodsRobinITR555-981290000 37JonesMaryFINC555-123456000 Employees Table SEX M F M F F M M F 32 37 36 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
43
Intersect Consider: Set R1 { a, b, d, f, h} Set R2 {b, c, f, k, p} Which elements are common between the two sets?
44
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
45
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'; 23 32 37 36 JAVA programmers COBOL programmers
46
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'; 23 32 37 36 JAVA programmers COBOL programmers
47
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' 23 32 37 36 JAVA programmers COBOL programmers
48
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.
49
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.
50
Need for CASE statement PROFICIENCY EXCELLENT GOOD EXCELLENT 31 32 EMPID FAIR37 23 36 SELECT empID, proficiency FROM programs WHERE Language = 'SQL' ORDER BY proficiency;
51
Use of CASE statement PROFICIENCY EXCELLENT GOOD EXCELLENT 31 32 EMPID FAIR37 23 36 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);
52
Use of old DECODE statement PROFICIENCY EXCELLENT GOOD EXCELLENT 31 32 EMPID FAIR37 23 36 SELECT empID, proficiency FROM programs WHERE Language = 'SQL' ORDER BY DECODE(proficiency, 'EXCELLENT', 1, 'GOOD',2, 'FAIR',3 );
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.