Beruflich Dokumente
Kultur Dokumente
SQL
SQL
1999-2001, NCST
Components of SQL:
Data Definition Component
a.k.a. Data Definition Language (DDL) -----------------
SQL: Syntax
SQL: the standard SQL2 (SQL92)
v/s
SQL: implementations
SQL*Plus, mySQL, SQL Server . . . .
Although the implementations are built around the SQL standard, they have their own nuances.
SQL in action:
Single table queries Multiple table queries Aliases Subqueries Aggregate functions
1999-2001, NCST 4
SQL
Emp Table
Emp Emp Emp Emp Dept Dept Code Name Code Code Name Code Birth Birth Date Date Join Join Sex Desig Sup Desig Sup Sex Code Code Date Date Code Code
Grade Grade Basic Grade Grade Basic Code Lvl Pay Code Lvl Pay
SQL
1999-2001, NCST
Dept Table
DeptCode DeptCode DeptName DeptName DeptManager DeptManager DeptBudget DeptBudget
Salary Table
EmpCode EmpCode SalMonth SalMonth Basic Basic Allow Allow Deduct Deduct
SQL
1999-2001, NCST
Desig Table
DesigCode DesigCode DesigName DesigName
History Table
EmpCode EmpCode ChangeDate ChangeDate DesigCode DesigCode GradeCode GradeCode BasicPay BasicPay GradeLevel GradeLevel
SQL
1999-2001, NCST
SQL
1999-2001, NCST
DEPTNAME DEPTMA DEPTBUDGET ----------- ------ ---------Accounts 7839 19 Purchase 7902 25 Sales 7698 39 Stores 7521 33 Facilities 7233 42 Personnel 7233 12
6 rows selected.
A Better Way!
2
List the Department table
SELECT FROM
SQL
1999-2001, NCST
10
SQL
1999-2001, NCST
11
SELECT FROM
SQL
1999-2001, NCST
12
6 rows selected.
SQL
1999-2001, NCST
13
EmpName, DeptCode Emp DeptCode = 'ACCT'; EMPNAME ---------Reddy Menon Kaul DEPT ---ACCT ACCT ACCT
14
SQL
1999-2001, NCST
SQL
1999-2001, NCST
15
EMPNAME GRADECODE ---------- --------Naik 4 Reddy 1 Murthy 4 Wilson 4 Jain 4 Menon 4 Khan 6 Kumaran 4 Kamal 4 9 rows selected.
SQL
1999-2001, NCST
16
Date Comparison
6
List all young employees
SQL
1999-2001, NCST
17
8 rows selected.
SQL
1999-2001, NCST
18
Set Membership
7
List employees of admin departments
SELECT EmpName, DeptCode FROM Emp WHERE DeptCode in ( 'ACCT' , 'PRCH' , 'PERS' );
SQL
1999-2001, NCST
19
EMPNAME ---------Shah Naik Reddy Jain Menon Khan Patil Kaul Uma
DEPT ---PRCH PRCH ACCT PRCH ACCT PRCH PRCH ACCT PERS
9 rows selected.
SQL
1999-2001, NCST
20
Between Construct
8
List all middle level staff SELECT EmpName, GradeCode FROM Emp WHERE GradeCode BETWEEN 10 AND 15; EMPNAME GRADECODE ---------- --------Roy 12 Gupta 12 Singh 12 Uma 15
SQL 1999-2001, NCST 21
Like Construct
9
List all employees with UMA in their names SELECT EmpName FROM Emp WHERE Upper(EmpName) LIKE '%UMA%'; EMPNAME ---------Kumaran Uma
SQL 1999-2001, NCST 22
Null Values
10
List the employees who have not been assigned to any supervisor
SELECT EmpName, SupCode FROM Emp WHERE SupCode IS NULL; EMPNAME SUPCOD ---------- -----Reddy
SQL 1999-2001, NCST 23
Predicate using OR
12
List employees who are either 50 years or more or have more than 20 years experience EmpName Emp BirthDate < (sysdate - 50*365) JoinDate < (sysdate - 20*365); EMPNAME ---------Reddy Kumaran Kamal
SQL 1999-2001, NCST 25
Parentheses in Predicate
13
List foremen who are either 50 years or more or have more than 20 years' experience EmpName Emp DesigCode = 'FRMN' (BirthDate < (sysdate - 50*365) JoinDate < (sysdate - 20*365)); EMPNAME ---------Kumaran Kamal
SQL 1999-2001, NCST 26
SELECT EmpCode, (Basic + Allow - Deduct) * 0.01 FROM Salary WHERE SalMonth = '1-Mar-97';
SQL
1999-2001, NCST
27
EMPCOD (BASIC+ALLOW-DEDUCT)*0.01 ------ ------------------------7129 440 7233 440 7345 143 7369 66 -------------------------------7844 198 7876 44 7900 55 7902 330 7934 77 17 rows selected.
SQL 1999-2001, NCST 28
Aliasing
15
List the present age of all the employees
SQL
1999-2001, NCST
29
EMPNAME AGE ---------- --------Shah 20 Naik 36 Reddy 54 Murthy 34 Roy 24 -------------------Shroff 19 Kaul 21 Kumaran 57 Kamal 46 Uma 22 17 rows selected.
SQL 1999-2001, NCST 30
Aggregate Functions
16
Count employees reporting to Singh
ORDER BY
Emp Emp Name Name Grade Grade Code Code
3 3 2 2 4 1 2
No particular order
SQL 1999-2001, NCST 32
ORDER BY
SELECT ....... FROM ....... ....... ...... ORDER BY gradecode;
Emp Emp Name Name Grade Grade Code Code
No ordering of EmpName
SQL 1999-2001, NCST
1 2 2 2 3 3 4
33
ORDER BY
SELECT ....... FROM ....... ....... ...... ORDER BY gradecode, empname;
Emp Emp Name Name Grade Grade Code Code
1 2 2 2 3 3 4
SQL
1999-2001, NCST
34
Order by
17
SELECT List all employees ordered by age
EmpName, TRUNC( (SYSDATE - BirthDate) / 365) AGE FROM Emp ORDER BY BirthDate;
SQL
1999-2001, NCST
35
EMPNAME AGE ---------- --------Kumaran 57 Reddy 54 Kamal 46 Menon 38 -------------------Uma 22 Patil 21 Kaul 21 Shah 20 Shroff 19 17 rows selected.
SQL 1999-2001, NCST 36
Sorting Ascending/Descending
18
List middle level staff according to seniority
EmpName, GradeCode, GradeLevel Emp GradeCode BETWEEN 10 AND 25 GradeCode, GradeLevel DESC;
SQL
1999-2001, NCST
37
EMPNAME GRADECODE GRADELEVEL ---------- --------- ---------Gupta 12 3 Roy 12 2 Singh 12 1 Uma 15 2 Patil 20 4 Shroff 20 3 Shah 20 2 Kaul 20 1 8 rows selected.
SQL
1999-2001, NCST
38
SELECT
EmpName, TRUNC( (SYSDATE - BirthDate) / 365) AGE, GradeCode, GradeLevel FROM Emp ORDER BY 2 DESC, GradeCode, GradeLevel DESC;
SQL
1999-2001, NCST
39
EMPNAME AGE GRADECODE GRADELEVEL ---------- --------- --------- ---------Kumaran 57 4 1 Reddy 54 1 1 Kamal 46 4 1 Menon 38 4 1 Wilson 37 4 4 -----------------------------------------Patil 21 20 4 Kaul 21 20 1 Shah 20 20 2 Shroff 19 20 3 17 rows selected.
SQL 1999-2001, NCST 40
SQL
1999-2001, NCST
41
SUPCOD COUNT(*) ------ --------7566 1 7698 5 7782 1 7788 1 7839 6 7844 1 7902 1 1 8 rows selected.
SQL
1999-2001, NCST
42
Group By - Sum
21
List the total take-home pay during 96-97 for all employees
SELECT
EmpCode, SUM( Basic + Allow - Deduct) PAY FROM Salary WHERE SalMonth BETWEEN '1-Apr-96' AND '31-Mar-97' GROUP BY EmpCode ORDER BY EmpCode;
SQL
1999-2001, NCST
43
EMPCOD PAY ------ --------7129 132000 7233 132000 7345 42900 7369 19800 7499 56100 ---------------7876 13200 7900 16500 7902 99000 7934 15400 17 rows selected.
SQL 1999-2001, NCST 44
SQL
1999-2001, NCST
45
GRADECODE MAX(BASIC) MIN(BASIC) --------- ---------- ---------1 25000 25000 4 21000 15000 6 13000 11000 12 9000 8000 15 7000 6000 20 3500 2000 6 rows selected.
SQL
1999-2001, NCST
46
Having
23
List the number of staff reporting to each supervisor having more than 3 people working under them SupCode, COUNT(*) Emp SupCode COUNT(*) > 3 SupCode; SUPCOD COUNT(*) ------ --------7698 5 7839 6
1999-2001, NCST 47
SQL
SELECT
EmpCode, SUM( Basic + Allow - Deduct) PAY FROM Salary WHERE SalMonth BETWEEN '1-Apr-96' AND '31-Mar-97' GROUP BY EmpCode HAVING SUM( Basic + Allow - Deduct) < 20000 ORDER BY EmpCode;
SQL 1999-2001, NCST 48
EMPCOD PAY ------ --------7369 19800 7876 13200 7900 16500 7934 15400
SQL 1999-2001, NCST 49
Natural Join
26
Check the basic salary of all employees
EmpCode, EmpName, Grade.Basic, Emp.Basic EMP, GRADE Emp.GradeCode = Grade.GradeCode Emp.GradeLevel = Grade.GradeLevel;
SQL
1999-2001, NCST
51
EMPCOD EMPNAME BASIC BASIC ------ ---------- --------- --------7369 Shah 3000 3000 7902 Naik 15000 15000 7839 Reddy 25000 25000 7698 Murthy 17000 17000 7499 Roy 8500 8500 ------------------------------------7934 Kaul 3500 3500 7233 Kumaran 20000 20000 7129 Kamal 20000 20000 7345 Uma 6500 6500 17 rows selected.
SQL 1999-2001, NCST 52
Self Join
27
List employees along with the names of their supervisors
SELECT E.EmpCode, E.EmpName, S.EmpCode, S.EmpName FROM EMP E, EMP S WHERE E.SupCode = S.EmpCode;
SQL
1999-2001, NCST
53
EMPCOD EMPNAME EMPCOD EMPNAME ------ ---------- ------ ---------7369 Shah 7902 Naik 7902 Naik 7839 Reddy 7698 Murthy 7839 Reddy 7499 Roy 7698 Murthy 7521 Wilson 7698 Murthy -------------------------------7900 Shroff 7698 Murthy 7934 Kaul 7782 Menon 7233 Kumaran 7839 Reddy 7129 Kamal 7839 Reddy 7345 Uma 7844 Singh 16 rows selected.
SQL 1999-2001, NCST 54
Deceptive Construction
28
List the number of officers reporting to each supervisor having more than 3 people working under them. SupCode, COUNT(*) EMP GradeCode < 10 SupCode COUNT(*) > 3; SUPCOD COUNT(*) ------ --------7839 6
1999-2001, NCST 55
SQL
Check again!!
29
List the number of officers reporting to each supervisor having more than 3 people working under them.
SELECT SupCode, COUNT(*) FROM EMP WHERE GradeCode < 10 SUPCOD COUNT(*) AND SupCode IN ------ --------(SELECT SupCode 7698 1 FROM EMP 7839 6 GROUP BY SupCode HAVING COUNT(*) > 3 ) GROUP BY Supcode;
SQL 1999-2001, NCST 56
Views - Creation
30
Create a view for Employee-Age
CREATE VIEW EMPAGE (EmpCode, Age) AS ( SELECT EmpCode, TRUNC ((SYSDATE - BirthDate)/ 365 ) FROM EMP );
SQL
1999-2001, NCST
57
Views - Creation
31
Create a view for Employee-Pay
CREATE VIEW EMPPAY (EmpCode, NetPay, SalMonth) AS ( SELECT EmpCode, (Basic + Allow - Deduct), SalMonth FROM SALARY );
SQL
1999-2001, NCST
58
Use of Views
32
List employees who are older than their supervisors.
SELECT E.EmpCode, EmpName FROM EMP E, EMPAGE A WHERE E.EmpCode = A.EmpCode AND age > ( SELECT Age EMPCOD FROM EMPAGE -----WHERE E.SupCode = EmpCode ); 7521 7233
SQL 1999-2001, NCST
Use of Views
33
List employees who were promoted to officer grade in 1995. SELECT EmpCode, EmpName, DeptCode, GradeCode, GradeLevel FROM EMP WHERE EmpCode IN ( SELECT EmpCode FROM HISTORY WHERE GradeCode >= 10 AND HistDate BETWEEN '01-Jan-95' AND '31-Dec-95'); EMPCOD EMPNAME DEPT GRADECODE GRADELEVEL ------ ---------- ---- --------- ---------7369 Shah PRCH 20 2
SQL 1999-2001, NCST 60
Nested Queries
34
List employees who did not get any promotion since 1990.
SELECT EmpCode, EmpName, DeptCode FROM EMP WHERE NOT EXISTS ( SELECT * FROM HISTORY WHERE EMP.EmpCode = EmpCode EMPCOD EMPNAME AND HistDate > '01-Jan-90'); ------ ---------7788 Khan 7876 Patil 7233 Kumaran
SQL 1999-2001, NCST
SQL
1999-2001, NCST
62
INSERT INTO EMP (EmpCode, EmpName, Basic) VALUES ('9123', 'Hussein', 250);
SQL
1999-2001, NCST
63
INSERT INTO SALARY SELECT EmpCode, trunc(sysdate, mon), Basic, Basic*1.5, Basic*0.3 FROM EMP;
SQL
1999-2001, NCST
64
SQL
1999-2001, NCST
65
Bulk Delete
39
Delete all employee records of Filing Department.
SQL
1999-2001, NCST
66
SQL
1999-2001, NCST
67
UPDATE EMP SET GradeCode = '4', DesigCode = 'MNGR', Basic = 15000 WHERE EmpCode = '7654;
SQL
1999-2001, NCST
68
Bulk Update
42
Raise the budget by 25% for all the departments except Facilities department.
SQL
1999-2001, NCST
69
EXISTS
43
List employees who did not get any promotion since 1990.
EmpCode, EmpName, DeptCode EMP NOT EXISTS (promotion records for him since 1990);
SQL
1999-2001, NCST
70
EXISTS
43
List employees who did not get any promotion since 1990.
EmpCode, EmpName, DeptCode EMP as E NOT EXISTS (SELECT * FROM HISTORY WHERE E.EmpCode = EmpCode AND HistDate >= date 1990-01-01);
1999-2001, NCST 71
SQL
DISTINCT
44
List all department names that begin with PR
72
DISTINCT
44
List all department names that begin with PR
DEPT ---PRCH
SQL
1999-2001, NCST
73
Updatable Views
3 3
no DISTINCT, GROUP BY, HAVING clauses select list contains only column references and that too only once from clause identifies only one base table
SQL
1999-2001, NCST
74
SQL
1999-2001, NCST
75
Sex
char(1) not null check (sex in ('M', 'F')), DesigCode char(4) not null constraint emp_desig_rc references desig(DesigCode) , SupCode char(6) constraint emp_sup_rc references emp(EmpCode) , GradeCode number(2), Basic number(5));
SQL 1999-2001, NCST 77