0% found this document useful (0 votes)
261 views49 pages

Introduction To SQL: By: Sonal Pandey M.E. CSE Dept. NITTTR, Chandigarh

Luke 101 Peter 102

Uploaded by

sonal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
261 views49 pages

Introduction To SQL: By: Sonal Pandey M.E. CSE Dept. NITTTR, Chandigarh

Luke 101 Peter 102

Uploaded by

sonal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

INTRODUCTION TO SQL

By : Sonal Pandey
M.E. CSE Dept.
NITTTR,Chandigarh
WHAT IS SQL?

 Structured Query Language


 SQL is Structured Query Language, which is a computer
language for storing, manipulating and retrieving data
stored in relational database.

 SQL is the standard language for Relation Database System.


All relational database management systems like “MySQL,
MS Access, Oracle, Sybase, Informix, postgres and SQL
Server” use SQL as standard database language.
SQL COMMANDS

 DDL - Data Definition Language

 DML - Data Manipulation Language

 DCL - Data Control Language

 DQL - Data Query Language


SQL COMMANDS
SQL COMMANDS
SQL RDBMS CONCEPTS

 TABLE
 RECORD
 COLUMN
 CELL

 NULL?
SQL RDBMS CONCEPTS
 SQL Constraints: (applied on columns)
 NOT NULL Constraint
 UNIQUE Constraint
 PRIMARY Key
 FOREIGN Key
 CHECK Constraint
 Data Integrity:
 Entity Integrity: There are no duplicate rows in a table
 Domain Integrity: Enforces valid entries for a given column
by
restricting the type
 Referential Integrity: Rows cannot be deleted which are used by
other records
 User-Defined Integrity: Enforces some specific business rules
SQL RDBMS CONCEPTS
 Data Types:
 Character datatypes:
 CHAR
 NCHAR
 NVARCHAR2
 VARCHAR2
 Numeric datatypes:
 NUMBER
 BINARY_FLOAT
 BINARY_DOUBLE
 Date time datatype:
 DATE
DCL: CREATE USER

 1) Define User with Username/Password


 2) Grants Sufficient Privileges.

 Using SQL Developer (UI, Command)


 Using SQL *PLUS (Command)
SQL : DDL
 Create Table:
 COLUMNS
 CONSTRAINTS
SQL: DDL

 DROP
TABLE:

 ALTER
TABLE

 OTHER DDL
COMMANDS!!!!
SQL: DML: INSERT
 INSERT INTO:
 NUMBER, CHAR/VARCHAR2,
DATE?
SQL: DML: INSERT
 INSERT INTO:
 SPECIFIC
COLUMNS
SQL: DML: UPDATE

UPDATE

 WHERE
CLAUSE
SQL: DML:
DELETE
 DELETE (DELETE VS
DROP????)
SQL: DQL: SELECT

SELECT
 *

 SPECIFIC
COLUMNS
SQL: DQL:
SELECT
 DISTINCT
CLUASE
SQL: DQL:
SELECT
 WHERE
CLUASE
SQL: DQL: SELECT
IN
CLUASE
 NOT IN

 NULL IN
WHERE?
 IS NULL
 IS NOT NULL
SQL: DQL:
SELECT
 ORDER
BY
SQL: DQL: SELECT
 GROUP BY

 AGGREGATE
FUCTION
 MAX,MIN
 AVG
 COUNT
 SUM
 …
 HAVING CLAUSE
SQL: DQL: SELECT
 SELECT FROM MULTIPLE
TABLES
 CARTESIAN MULTIPICATION

 JOIN
BASIC STRUCTURE OF SQL QUERY

General SELECT, ALL / DISTINCT, *,


Structure AS, FROM, WHERE

Comparison IN, BETWEEN, LIKE "% _"

Grouping GROUP BY, HAVING,


COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )

Display Order ORDER BY, ASC / DESC

Logical AND, OR, NOT


Operators

Output INTO TABLE / CURSOR


TO FILE [ADDITIVE], TO PRINTER, TO SCREEN

Union UNION
II Comparison
expr IN ( value1, value2, value3)
expr BETWEEN value1 AND value2
expr LIKE "%_"
II
eg. 6
COMPARISON
List the students who were born on Wednesday or Saturdays.

SELECT name, class, CDOW(dob) AS bdate ;


FROM student ;
WHERE DOW(dob) IN (4,7)
nam class bdate
Result ePeter 1A Wednesday
Wendy 1B Wednesday
Kevin 1C Saturday
Luke 1A Wednesday
Aaron 1A Saturday
: : :
II
eg. 7
COMPARISON
List the students who were not born in January, March, June,
September.

SELECT name, class, dob FROM student ;


WHERE MONTH(dob) NOT IN (1,3,6,9)
name class dob
Result Wendy 1B 07/09/86
Tobe 1B 10/17/86
Eric 1C 05/05/87
Patty 1C 08/13/87
Kevin 1C 11/21/87
Bobby 1A 02/16/86
Aaron 1A 08/02/86
: : :
II COMPARISON
eg. 8 List the 1A students whose Math test score is between 80 and 90
(incl.)

SELECT name, mtest FROM student ;


WHERE class="1A" AND ;
mtest BETWEEN 80 AND 90

Result nam mtes


eLuke t
86
Aaron 83
Gigi 84
II COMPARISON
eg. 9 List the students whose names start with "T".

SELECT name, class FROM student ;


WHERE name LIKE "T%"

Result name class


Tob 1B
eTeddy 1B
Tim 2A
II COMPARISON
eg. 10 List the Red house members whose names contain "a" as the 2nd
letter.

SELECT name, class, hcode FROM student ;


WHERE name LIKE "_a%" AND hcode="R"

name class hcode


Result
Aaron 1A R
Janet 1B R
Paula 2A R
III GROUPING
SELECT ...... FROM ...... WHERE condition ;
GROUP BY groupexpr [HAVING requirement]

Group functions:
COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )

– groupexpr specifies the related rows to be grouped


as one entry. Usually it is a column.
– WHERE condition specifies the condition of
individual rows before the rows are group.
HAVING requirement specifies the condition
involving the whole group.
III GROUPING
eg. 11 List the number of students of each class.
GROUP BY
CLASS
class
1A

1A 1A
1A
COUNT( )

1B
1B

1B
1B COUNT( )
1B
1B
1B
1C

1C 1C
1C
COUNT( )

Student
III GROUPING
eg. 11 List the number of students of each class.
SELECT class, COUNT(*) FROM student ;
GROUP BY class
class cn
1A t 10
Result
1B 9
1C 9
2A 8
2B 8
2C 6
III GROUPING
eg. 12 List the average Math test score of each class.
Group By Class
class
1A

1A 1A
1A
AVG( )
1B
1B

1B
1B
AVG( )
1B
1B
1B
1C

1C 1C AVG( )
1C
Student
III GROUPING
eg. 12 List the average Math test score of each class.

SELECT class, AVG(mtest) FROM student ;


GROUP BY class
class avg_mtest
Result 1A 85.90
1B 70.33
1C 37.89
2A 89.38
2B 53.13
2C 32.67
III GROUPING
eg. 13 List the number of girls of each district.

SELECT dcode, COUNT(*) FROM student ;


WHERE sex="F" GROUP BY dcode

Result dcod cnt


eHHM 6
1
KWC
MKK 1
SSP 5
TST 4
YMT 8
III GROUPING
eg. 14 List the max. and min. test score of Form 1 students of each
district.

SELECT MAX(mtest), MIN(mtest), dcode ;


FROM student ;
WHERE class LIKE "1_" GROUP BY
dcode
max_mtest min_mtest
Result dcode
92
91 19 36
HHM
91 MKK
31 SSP
92 36 TST
75 75 TSW
88 38 YMT
III GROUPING
eg. 15 List the average Math test score of the boys in each class. The list
should not contain class with less than 3 boys.

SELECT AVG(mtest), class FROM student ;


WHERE sex="M" GROUP BY class ;
HAVING COUNT(*) >= 3
Result avg_mtest
class
86.00
1A
77.75
1B
35.60
1C
86.50
2A
56.50
2B
IV DISPLAY ORDER
SELECT ...... FROM ...... WHERE ......
GROUP BY ..... ;
ORDER BY colname ASC / DESC
IV DISPLAY ORDER
eg. 16 List the boys of class 1A, order by their names.

SELECT name, id FROM student ;


WHERE sex="M" AND class="1A" ORDER BY name

name id Result name id


Peter 9801 Aaron 9812
Johnny 9803 Bobby 9811
ORDER BY
Luke 9810 Johnny 9803
dcode Luke 9810
Bobby 9811
Aaron 9812 Peter 9801
Ron 9813 Ron 9813
IV DISPLAY ORDER
eg. 17 List the 2A students by their residential district.
SELECT name, id, class, dcode FROM student ;
WHERE class="2A" ORDER BY dcode
name id class
Result dcode
Jimmy 9712 2A HHM
Tim 9713 2A HHM
Samual 9714 2A
SHT Rosa 9703 2A SSP
Helen 9702 2A TST
Joseph 9715 2A TSW
Paula 9701 2A
YMT
IV DISPLAY ORDER
eg. 18 List the number of students of each district
(in desc. order).

SELECT COUNT(*) AS cnt, dcode FROM student ;


GROUP BY dcode ORDER BY cnt DESC
cnt docode
Result 11 YMT
10 HHM
10 SSP
9 MKK
5 TST
2 TSW
1 KWC
1 MMK
1 SHT
IV DISPLAY ORDER
eg. 19 List the boys of each house order by the classes. (2-level
ordering)

SELECT name, class, hcode FROM student ;


WHERE sex="M" ORDER BY hcode, class
IV DISPLAY ORDER
Result
nam hcod class
eBobby eB 1A
Blue
House Teddy B 1B Order
Joseph B 2A by
Zio B 2B class
Order nLeslie B 2C
by Johnny G 1A
hcode Luk G 1A
eKevi G 1C
Green
nGeorge G 1C
House : : :
:
V OUTPUT

INTO TABLE tablename


the output table is saved as a
database file in the disk.

INTO CURSOR temp


the output is stored in the
working memory temporarily.

TO FILE filename [ADDITIVE]


output to a text file.
(additive =
append)
TO PRINTER send to printer.

TO SCREEN display on screen.


V
eg. 20
OUTPUT
List the students in desc. order of their names and save the result
as a database file name.dbf.

SELECT * FROM student ;


ORDER BY name DESC INTO TABLE name.dbf
Result id name dob sex class mtest hcode dcode remission
9707 Zion 07/29/85 M 2B 51 B MKK .F.
9709 Yvonne 08/24/85 F 2C 10 R TST .F.
9804 Wendy 07/09/86 F 1B 84 B YMT .F.
9819 Vincent 03/15/85 M 1C 29 Y MKK .F.
9805 Tobe 10/17/86 M 1B 88 R YMT .F.
9713 Tim 06/19/85 M 2A 91 R HHM .T.
9816 Teddy 01/30/86 M 1B 64 B SSP .F.
: : : : : : : : :
V
eg. 21
OUTPUT
Print the Red House members by their classes, sex and name.

SELECT class, name, sex FROM student ;


WHERE hcode="R" ;
ORDER BY class, sex DESC, name TO
PRINTER class name sex
Result 1A Aaron M
1A Peter M
1A Ron M
1B Tobe M
1B Janet F
1B Kitty F
1B Mimi F
: : :
THANKYOU

You might also like