Download presentation
Presentation is loading. Please wait.
Published byClaude Harmon Modified over 9 years ago
1
1 Querying a Single Table Structured Query Language (SQL) - Part II
2
2 SELECT l SELECT allows you to query the database l We will first consider SELECT statements with one table SELECT [DISTINCT | ALL] {* | select_list} FROM{table_name [alias] | view_name} [{table_name [alias] | view_name}] … [WHEREcondition] [GROUP BYcondition_list] [HAVINGcondition] [ORDER BY {column_name [ASC | DESC]} …]
3
3 SELECT Employee Table EIDEnameDepartmentSalary 1001JohnFinance40000 1002JacobFinance45000 1003JoeAccounting35000 1004JaneMarketing50000 1005JillAccounting30000 1006JebMarketing35000 1007JudyInfo Tech40000 1008JoshInfo Tech50000 SELECT EID, Ename FROM Employee; EIDENAME 1001John 1002Jacob 1003Joe 1004Jane 1005Jill 1006Jeb 1007Judy 1008Josh
4
4 SELECT l Rules for specifying column names u column names must match the name in the data dictionary u column names must be separated by a comma u column names selected must belong to the table named in the FROM clause u Column names may be specified using mixed case - the system is not case sensitive
5
5 SELECT SELECT * FROM Employee; EIDEnameDepartmentSalary 1001JohnFinance40000 1002JacobFinance45000 1003JoeAccounting35000 1004JaneMarketing50000 1005JillAccounting30000 1006JebMarketing35000 1007JudyInfo Tech40000 1008JoshInfo Tech50000 SELECT ename name, salary FROM Employee; NameSalary John40000 Jacob45000 Joe35000 Jane50000 Jill30000 Jeb35000 Judy40000 Josh50000 A column alias All Columns
6
6 SELECT Department Finance Accounting Marketing Accounting Marketing Info Tech SELECT department FROM Employee; SELECT DISTINCT department FROM Employee; Use of the DISTINCT clause, the default is ALL Department Accounting Finance Info Tech Marketing
7
7 SELECT with WHERE Clause l WHERE clause is followed by a condition l Oracle offers nine different comparison operators: u = >= <= u != <> !> !< l Character strings or date values must be enclosed within single quotation marks
8
8 SELECT with WHERE Clause SELECT * FROM Employee WHERE salary > 40000; EIDEnameDepartmentSalary 1002JacobFinance45000 1004JaneMarketing50000 1008JoshInfo Tech50000 SELECT * FROM Employee WHERE department = Accounting; Error - Invalid Column Name
9
9 SELECT with WHERE Clause SELECT * FROM Employee WHERE department = “Accounting”; Error - Invalid Column Name /* Enclose char data within single quotes /* SELECT * FROM Employee WHERE department = ‘Accounting’; EIDEnameDepartmentSalary 1003JoeAccounting35000 1005JillAccounting30000
10
10 AND Condition SELECT ename, salary FROM Employee WHERE department = ‘Finance’ AND salary > 40000; EnameSalary Jacob45000 SELECT * FROM Employee WHERE department = ‘Accounting’ AND salary > 40000; no rows selected
11
11 OR Condition SELECT ename, department, salary FROM Employee WHERE department = ‘Finance’ OR department = ‘Marketing’; EnameDepartmentSalary JohnFinance40000 JacobFinance45000 JaneMarketing50000 JebMarketing35000
12
12 Combining AND with OR /* Be careful when you combine AND with OR */ SELECT ename, department, salary FROM Employee WHERE department = ‘Finance’ OR department = ‘Marketing’ AND salary > 40000; EnameDepartmentSalary JohnFinance40000 JacobFinance45000 JaneMarketing50000
13
13 Combining AND with OR SELECT ename, department, salary FROM Employee WHERE (department = ‘Finance’ OR department = ‘Marketing’) AND salary > 40000; EnameDepartmentSalary JacobFinance45000 JaneMarketing50000
14
14 ORDER BY Clause l ORDER BY is used when you want to sort the result of a query l ORDER BY is followed by one or more column names (max 16) separated by commas l The default sorting order is ascending (low to high) l You can sort the result in descending order (high to low) by using the optional keyword DESC l ASC or DESC keywords follow the respective column names specified in the ORDER BY clause
15
15 ORDER BY Clause SELECT ename, deaprtment, salary FROM Employee WHERE salary >= 40000 ORDER BY department; EnameDepartmentSalary JohnFinance40000 JacobFinance45000 JudyInfo Tech40000 JoshInfo Tech50000 JaneMarketing50000
16
16 ORDER BY Clause SELECT ename, deaprtment, salary FROM Employee WHERE salary >= 40000 ORDER BY salary desc, department; EnameDepartmentSalary JoshInfo Tech50000 JaneMarketing50000 JacobFinance45000 JohnFinance40000 JudyInfo Tech40000
17
17 Condition - IN and NOT IN l IN and NOT IN operators are used to create condition expressions that compare a column name to a set of values. l Values are listed within parentheses and are separated by commas l Character constants are enclosed within single quotes
18
18 Condition - IN and NOT IN SELECT ename, department FROM Employee WHERE department = ‘Accounting’ OR department = ‘Finance’ OR department = ‘Marketing’; SELECT ename, department FROM Employee WHERE department IN (‘Accounting’, ‘Finance’, ‘Marketing’); EnameDepartment JoeAccounting JillAccounting JohnFinance JacobFinance JaneMarketing JebMarketing
19
19 Condition - IN and NOT IN EnameDepartment JudyInfo Tech JoshInfo Tech SELECT ename, department FROM Employee WHERE department NOT IN (‘Accounting’, ‘Finance’, ‘Marketing’); /* The above query is equivalent to the following */ SELECT ename, department FROM Employee WHERE (department <> ‘Accounting’ AND department <> ‘Finance’ AND department <> ‘Marketing’);
20
20 BETWEEN and NOT BETWEEN l Between is used to specify an inclusive range for comparison SELECT ename, salary FROM Employee WHERE salary BETWEEN 35000 AND 45000; EnameSalary John40000 Jacob45000 Joe35000 Jeb35000 Judy40000 /* The above query is equivalent to the following */ SELECT ename, salary FROM Employee WHERE salary >= 35000 AND salary <= 45000;
21
21 BETWEEN and NOT BETWEEN SELECT ename FROM Employee WHERE ename BETWEEN ‘Joe’ AND ‘Josh’; Ename John Joe Josh SELECT ename, salary FROM Employee WHERE salary NOT BETWEEN 35000 AND 45000; NameSalary Jane50000 Jill30000 Josh50000
22
22 LIKE - String and Character Matching l LIKE is used to match a character string (%) or a single character ( _ ) l You may mix % with _ in the same pattern /* Find names starting with Jo */ SELECT ename FROM Employee WHERE ename LIKE ‘Jo%’; Name John Joe Josh SELECT ename, salary FROM Employee WHERE salary LIKE ‘3_000’; NameSalary Joe35000 Jill30000 Jeb35000
23
23 LIKE - String and Character Matching /* Find 3 lettered names starting with J */ SELECT ename FROM Employee WHERE ename LIKE ‘J__ ’; Name Joe Jeb /* LIKE treats a blank space as a character */ /* You must pad the pattern with blank spaces for proper execution of this query */ SELECT ename FROM Employee WHERE ename LIKE ‘J__ ’; No rows selected Note: J followed by 3 underscores and required number of blank spaces will select all 1,2,3 and 4 lettered names starting with J.
24
24 NULL Values l A NULL value is treated as undefined l If it participates in an arithmetic operation the result is always NULL l NULL values can be checked using the statement u WHERE IS [NOT] NULL SalesAgent IDNameSalaryBonus 1001John Super2000050000 1002Jeb Broke10000NULL 1003Super Seller2000060000 SELECT name FROM SalesAgent WHERE bonus IS NULL; Name Jeb Broke
25
25 NULL Values SELECT Name, Salary+Bonus FROM SalesAgent; NameSalary+Bonus John Super 70000 Jeb Broke NULL Super Seller 80000 SELECT Name FROM SalesAgent WHERE bonus IS NOT NULL; Name John Super Super Seller
26
26 AGGREGATE FUNCTIONS l COUNT, SUM, AVG, MAX, MIN l COUNT (*) : returns the number of selected rows l COUNT ([ALL | DISTINCT] ) u ALL is the default u DISTINCT count distinct values u Usually expression refers to a column name u NULL values in expression are not counted - so it counts all (distinct) non null values in the expression. SalesAgent IDNameSalaryBonus 1001John Super2000050000 1002Jeb Broke10000NULL 1003Super Seller2000060000
27
27 COUNT - Examples SELECT COUNT(*) FROM SalesAgent WHERE salary > 15000; Count(*) 2 SELECT COUNT(DISTINCT salary) FROM SalesAgent; Count(Distinct salary) 2 /* Null value is not counted */ SELECT COUNT(bonus) FROM SalesAgent; Count(bonus) 2 SELECT COUNT(salary) FROM SalesAgent; Count(salary) 3 SELECT COUNT(*) FROM SalesAgent; Count(*) 3
28
28 SUM l SUM ([ALL | DISTINCT] expression) u total of all (distinct) values in a numeric column u NULL values are excluded from computation u empty column returns NULL SELECT SUM(salary) FROM SalesAgent; Sum(salary) 50000 SELECT SUM(DISTINCT salary) FROM SalesAgent; Sum(Distinct salary) 30000
29
29 SUM /* Compute the total salary and bonus paid to all salesagents */ Sum(salary+bonus) 150000 SELECT SUM(salary+bonus) FROM SalesAgent; SELECT SUM(salary) + SUM(bonus) FROM SalesAgent; Sum(salary)+Sum(bonus) 160000 Why are the above results different? /* Total salary paid to employees in the Finance department */ SELECT SUM(salary) FROM Employee WHERE department = ‘Finance’; Sum(salary) 95000
30
30 AVG l AVG ([ALL | DISTINCT] expression) u average of all (distinct) values in a numeric column u NULL values are excluded from computation l SUM(x)/COUNT(*) will give different result from AVG(x) when x has null values u empty column returns NULL SELECT AVG(salary) FROM SalesAgent; SELECT AVG(bonus) FROM SalesAgent; SELECT SUM(bonus)/COUNT(*) FROM SalesAgent; AVG(salary) 16666.6667 AVG(bonus) 55000 SUM(bonus)/COUNT(*) 36666.6667
31
31 MIN and MAX l MIN (expression) - the lowest value in the expression l MAX (expression) - the highest value in the expression l MIN and MAX work with numeric as well as character data SELECT MIN(salary) FROM SalesAgent; SELECT MAX(salary) FROM SalesAgent; SELECT MIN(name) FROM SalesAgent; MIN(salary) 10000 MAX(salary) 20000 MIN(name) Jeb Broke
32
32 GROUP BY l GROUP BY : allows grouping results l column name must be from the table specified in the FROM clause l produces one line in the result for each unique value in the specified column l if this column has null values null is treated as a group l No aggregate function is allowed in the GROUP BY clause u GROUP BY AVG(salary) is not permitted l You may specify an expression listed in the SELECT clause after Group By (see pp.36 Bordoloi)
33
33 GROUP BY- Examples Employee EIDEnameDepartmentSalary 1001JohnFinance40000 1002JacobFinance45000 1003JoeAccounting35000 1004JaneMarketing50000 1005JillAccounting30000 1006JebMarketing35000 1007JudyInfo Tech40000 1008JoshInfo Tech50000 l SQL standard does not require Group By to sort the result - though a specific implementation may do so l Result may be sorted using the ORDER BY clause l We will use the following table in the examples
34
34 GROUP BY- Examples SELECT department, COUNT(*), AVG(salary) FROM Employee GROUP BY department; DepartmentCOUNT(*)AVG(salary) Accounting 2 32500 Finance 2 42500 Info Tech 2 45000 Marketing 2 42500 SELECT department, COUNT(*), AVG(salary) FROM Employee GROUP BY department ORDER BY AVG(salary) DESC; DepartmentCOUNT(*)AVG(salary) Info Tech 2 45000 Finance 2 42500 Marketing 2 42500 Accounting 2 32500
35
35 GROUP BY with HAVING l HAVING condition l HAVING is used with GROUP BY. l HAVING clause uses group characteristics in the condition. l aggregate functions are not allowed in the WHERE clause. l HAVING applies a condition to groups the way WHERE applies a condition to rows.
36
36 HAVING - Example SELECT department, COUNT(*), AVG(salary) FROM Employee GROUP BY department HAVING AVG(salary) > 40000; DepartmentCOUNT(*)AVG(salary) Finance 2 42500 Info Tech 2 45000 Marketing 2 42500 SELECT department, AVG(salary) FROM Employee WHERE department != ‘Finance’ GROUP BY department HAVING AVG(salary) < 45000; DepartmentAVG(salary) Accounting 32500 Marketing 42500
37
37 Example SELECT department, AVG(salary) FROM Employee WHERE department != ‘Finance’ GROUP BY department HAVING AVG(salary) < 45000 ORDER BY AVG(salary) DESC; DepartmentAVG(salary) Marketing 42500 Accounting 32500
38
38 SELF TEST l Use the Student and the Majors table that you created before in the following queries u 1. List all rows in Student table u 2. List names, majors and gpas of all students u 3. List names, majors and gpas of all students sorted by major in ascending order and by gpa within major in descending order u 4. Print distinct major names from the student file u 5. Print student count and average gpa for all students u 6. Print student counts and average gpas by major u Continued ….
39
39 SELF TEST l Continued from previous slide u 7. Print names and gpas of all students that are INSY majors and have a gpa above 3.0 u 8. Print names and gpas of all students that are INSY or Marketing majors and have a gpa above 3.0 u 9. What are the minimum and maximum gpas? u 10. What are the minimum and maximum gpas of INSY majors?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.