Sie sind auf Seite 1von 79

Structured Query Language

SQL

SQL

1999-2001, NCST

Components of SQL:
Data Definition Component
a.k.a. Data Definition Language (DDL) -----------------

Data Manipulation Component

a.k.a. Data ----------------------- Language (DML) Manipulation

Transaction Control Component Others


View Definition, Dynamic DML, Integrity
SQL 1999-2001, NCST 2

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.

case insensitive free-format support for ad-hoc queries


SQL 1999-2001, NCST 3

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

The Whole Table


1
List the Department table

SELECT * FROM Dept;

SQL

1999-2001, NCST

DEPT ---ACCT PRCH SALE STOR FACL PERS

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

DeptCode, DeptName, DeptManager, DeptBudget Dept;

SQL

1999-2001, NCST

10

DEPTMA DEPTBUDGET ------ ---------7839 19 7902 25 7698 39 7521 33 7233 42 7233 12

SQL

1999-2001, NCST

11

Only Some Columns


3
List all department managers with the names of their departments

SELECT FROM

DeptManager, DeptName Dept;

SQL

1999-2001, NCST

12

DEPTMA -----7839 7902 7698 7521 7233 7233

DEPTNAME ------------------------Accounts Purchase Sales Stores Facilities Personnel

6 rows selected.

SQL

1999-2001, NCST

13

WHERE Predicate (=)


4
List all employees of the Accounts department

SELECT FROM WHERE

EmpName, DeptCode Emp DeptCode = 'ACCT'; EMPNAME ---------Reddy Menon Kaul DEPT ---ACCT ACCT ACCT
14

SQL

1999-2001, NCST

WHERE Predicate (<)


5
List all officers

SELECT EmpName, GradeCode FROM Emp WHERE GradeCode < 10;

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

SELECT EmpName, BirthDate FROM Emp WHERE BirthDate > '01-JAN-70';

SQL

1999-2001, NCST

17

EMPNAME ---------Shah Roy Gupta Singh Patil Shroff Kaul Uma

BIRTHDATE --------12-AUG-76 01-JUL-72 26-JUL-73 20-JAN-74 30-APR-75 14-NOV-77 26-MAY-75 14-JAN-75

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

Compound Predicate - Interval


11
List the female employees who have just completed 5 years EmpName, Sex, JoinDate Emp Sex = 'F' JoinDate BETWEEN (sysdate - 5*365) AND (sysdate - 6*365); EMPNAME S JOINDATE ---------- - --------Uma F 22-OCT-91
SQL 1999-2001, NCST 24

SELECT FROM WHERE AND

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

SELECT FROM WHERE OR

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 FROM WHERE AND OR

Expressions in SELECT List


14
List 1% of take-home pay of all employees

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

SELECT EmpName, TRUNC( (SYSDATE - BirthDate) / 365) AGE FROM Emp;

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

SELECT COUNT(*) FROM Emp WHERE SupCode = '7844'; COUNT(*) --------1


SQL 1999-2001, NCST 31

ORDER BY
Emp Emp Name Name Grade Grade Code Code

WILSON JAIN MURTHY MENON KHAN REDDY NAIK

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

REDDY MURTHY MENON NAIK WILSON JAIN KHAN

1 2 2 2 3 3 4

33

ORDER BY
SELECT ....... FROM ....... ....... ...... ORDER BY gradecode, empname;
Emp Emp Name Name Grade Grade Code Code

REDDY MENON MURTHY NAIK JAIN WILSON KHAN

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

SELECT FROM WHERE ORDER BY

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

Sorting by Result of Expression


19
List all according to age and seniority

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

Group By - Aggregate Functions


20
List the number of staff reporting to each supervisor

SELECT FROM GROUP BY ORDER BY

SupCode, COUNT(*) Emp SupCode SupCode;

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

Group By - Max & Min


22
List the maximum & minimum salaries in grades

SELECT FROM GROUP BY ORDER BY

GradeCode, MAX(Basic), MIN(Basic) Grade GradeCode GradeCode;

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

SELECT FROM GROUP BY HAVING ORDER BY

SQL

Where - Group By - Having


24
List the total take-home pay during 96-97 for all employees getting a total take-home-pay < Rs. 20000

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

Where - Group By - Having


24
List the total take-home pay during 96-97 for all employees getting a total take-home-pay < Rs. 20000

EMPCOD PAY ------ --------7369 19800 7876 13200 7900 16500 7934 15400
SQL 1999-2001, NCST 49

Where - Group By - Having


25
List the maximum and minimum basic salary in each grade for grades with start < Rs. 4000 GradeCode, MAX(Basic), MIN(Basic) Grade GradeCode MIN(Basic) < 4000 GradeCode; GRADECODE MAX(BASIC) MIN(BASIC) --------- ---------- ---------20 3500 2000
SQL 1999-2001, NCST 50

SELECT FROM GROUP BY HAVING ORDER BY

Natural Join
26
Check the basic salary of all employees

SELECT FROM WHERE AND

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

SELECT FROM WHERE GROUP BY HAVING

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

EMPNAME ---------Wilson Kumaran


59

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

DEPT ---PRCH PRCH FACL


61

INSERT one complete row


35
Promote Shah as Salesman.

INSERT INTO HISTORY VALUES ('7369','01-Aug-96','SLMN','12',5000);

SQL

1999-2001, NCST

62

INSERT one partial row


36
Employ Hussein as a temporary employee.

INSERT INTO EMP (EmpCode, EmpName, Basic) VALUES ('9123', 'Hussein', 250);

SQL

1999-2001, NCST

63

INSERT thru Subquery


37
Update the SALARY table for the month.

INSERT INTO SALARY SELECT EmpCode, trunc(sysdate, mon), Basic, Basic*1.5, Basic*0.3 FROM EMP;

SQL

1999-2001, NCST

64

Delete one row


38
Delete employee record of Kaul.

DELETE FROM EMP WHERE EmpCode = '7934;

SQL

1999-2001, NCST

65

Bulk Delete
39
Delete all employee records of Filing Department.

DELETE FROM EMP WHERE DeptCode = 'FLNG;

SQL

1999-2001, NCST

66

Delete entire Table


40
Delete the entire SALARY table.

DELETE FROM SALARY;

SQL

1999-2001, NCST

67

Update one Row


41
Promote Gupta as Manager(Exports).

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.

UPDATE SET WHERE

DEPT DeptBudget = DeptBudget * 1.25 DeptCode != 'FACL;

SQL

1999-2001, NCST

69

EXISTS
43
List employees who did not get any promotion since 1990.

SELECT FROM WHERE

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.

SELECT FROM WHERE

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

SELECT FROM WHERE

DeptName Dept DeptName LIKE PR%;

duplicates are NOT eliminated !


SQL 1999-2001, NCST

DEPT ---PRCH PRCH PRCH

72

DISTINCT
44
List all department names that begin with PR

SELECT FROM WHERE

DISTINCT DeptName Dept DeptName LIKE 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

DDL for Table Dept


create table dept ( DeptCode char(4) constraint dept_pk primary key, DeptName char(25) not null, DeptManager char(6), DeptBudget number not null);

SQL

1999-2001, NCST

75

DDL for Table Emp


create table emp ( EmpCODE char(6) constraint emp_pk primary key, EmpNAME char(20) not null, DeptCode char(4) constraint emp_dept_rc references dept(deptcode), BirthDate Date not null, JoinDate Date not null,
continued....
SQL 1999-2001, NCST 76

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

DDL for Table Grade


create table grade ( GradeCode number(2) not null, GradeLevel number not null, Basic number(5) not null, constraint grade_pk primary key (GradeCode,GradeLevel) );
Table-level constraint
SQL 1999-2001, NCST 78

DDL for Table Salary


create table salary ( EmpCode char(6) not null, SalMonth date not null, Basic number, Allow number, Deduct number not null, constraint salary_pk primary key (EmpCode, SalMonth) );
SQL 1999-2001, NCST 79

Das könnte Ihnen auch gefallen