You are on page 1of 59

SQL

• Data sub-language derived from Square


• Based on Relational algebra
• CREATE TABLE
• INSERT COMMAND
• SELECT COMMAND
• UPDATE COMMAND
• DELETE COMMAND
• ALTER TABLE
• CREATE TABLE table-name (field1
datatype1 [constraints1] [,field2 datatype2
[constraints2]]…)
Tables
• Student(sid,sname,sex,major,gpa)

• Faculty(fid,fname,dept,ext,rank,salary)

• Course(crsnbr,cname,credit,maxenrl,fid)

• Crsenrl(crsnbr,sid,grade)
Create Table
• CREATE TABLE table-name(field1 datatype1
[constraints11] [,constraints12]… [,field2 datatype2
constraints21]…)
• EXAMPLE
• Create table student(sid char(10) not null, primary key,
sname varchar(20),
sex char(1),
major char(3),
gpa decimal(5,2))
Student
Sid Snam Sex Major Gpa
e
Insert command
• INSERT INTO tablename(value1 [,value2]
…)

• Example:
• insert into
student(‘cs1’,’anand’,’m’,’cse’,7.00)
Student
• Sid Snam Sex Major Gpa
e
cs1 anand M cse 7.00
Student
• Sid sname sex major gpa
• 1 RAM M MEC 8.5
• 2 BOSE M CSE 9.0
• 3 CHANDRA F IT 6.5
• 4 DEVI F CSE 9.5
• 5 Elango M IT 7.2
Select sname, major, gpa
from student
where gpa > 7.0
order by major gpa desc
SELECT COMMAND
• Syntax
• SELECT [DISTINCT] {*/col1 [,col2]…}
FROM table-name
[WHERE search-condition]

Examples
Select sname from student
Select * from student
Select sname,major,gpa from student
SELECT DISTINCT MAJOR
FROM STUDENT
• SELECT {*/ col-1 [,col-2]…}
FROM table-name
[WHERE search-condition]
[ORDER BY col-1 [DESC] [,col-2 [DESC]]…]
COMPARISION OPERATORS
• =,<,<=,>,>=,<>,!=
• Examples
• Select sname from student
where major=‘cse’
Select sname,gpa
from student
where gpa > 7.5
LOGIC OPERATORS
• AND,OR,NOT
• Select sname,sex,gpa
from student
where major=‘CSE’ and gpa>7.5

• select sname,major,gpa
from student
where (major=‘cse’ or major=‘it’) and gpa>7.0
BETWEEN
Syntax
SELECT [distinct] {*/col1 [,col2]…}
FROM table-name
[WHERE expr [NOT] BETWEEN lower-value
AND upper-value

Example
Select sname,gpa
from student
where gpa not between 8.0 and 9.9
IN
• SELECT [distinct] {*/col1 [,col2]…}
FROM table-name
[WHERE expr [NOT] IN
(value1 [,value2]…)]

Example
Select sname,major
from student
where major not in (‘CSE’,’ECE’,’EEE’)
• Select name
from student
where name like ‘K_l_’;
LIKE
• Where col like ‘pattern’
• % ignores zero or more characters in the string
• _(underscore),ignores a single character in the
string.
• Select sname from student where sname from
like ‘p%’_all names starting with p.
• Pael
• Paul
• Patrick
Search condition using null
• Col is [not] null
• List the name of any student whose major
major is null;
• Select sname
from student
where major is null.
Sorting the results
• SELECT {*/ col-1 [,col-2]…}
FROM table-name
[WHERE search-condition]
[ORDER BY col-1 [DESC] [,col-2 [DESC]]…]
• Retrive a list of students in alphabetical
order .
• Your answer must display name,major and
gpa.
• Select sname ,major , gpa
from student
Order by sname
• Retrieve a list of all students with gpa
above 7.0 in the ascending order of major
and descending order of gpa. your result
should include the attributes sname, major
and gpa.
• Select sname,major,gpa
from student
where gpa>6.0
order by major gpa desc
• Retrieve a list of students arranged by
major and within major ,arranged by the
highest grade average first
• Select sname,major,gpa
from student
order by major gpa desc
Decs order of gpa with gpa>7.25

• Select sname,gpa,major
from student
where gpa > 7.75
order by sname gpa desc
• Make a faculty table
• Write a query to provide 10% increase in
salary to all the faculty members.

select fname,salary, salary * 1.1


from faculty
• Fid fname salary
• 100 A 100000
• 200 B 60000
• 300 CC 50000
• 400 DDD 200000
Fname
• A 100000 110000
• B 60000 66000
• CC 50000 55000
• DDD 200000 220000
• List the name of all faculty members earning an
yearly salary of above Rs 1000000
• _____________________________________
Select fname from faculty
Where (salary *12)>1000000
________________________________________
Fid fname Monthly_salary
100 AK 100000
200 SS 40000
300 KKK 90000
SQL AGGREGATE FUNCTIONS

• SUM( COLUMN NAME )


• AVG( “ )
• MAX( “ )
• MIN( “ )
• COUNT( * )

SELECT col-1[,col-2]…
FROM table-name
[WHERE search-condition]
[GROUP BY col-1[,col-2]….]
[ORDER BY col-1[DESC][,col-2[DESC]]…]
• What is the average salary paid to the each department
Select dept, avg(salary)
From faculty
Group by dept rank

fid fname dept rank salary


100 A CSE AP 100000
200 B CSE Prof 200000
300 C IT AP 60000
400 D IT AP 80000
500 E CSE Prof 300000
• Display the number of faculty members
and the avg salary paid to them for the
faculty in cse dept
• Select count (*),avg(salary)
• from faculty
• where dept =‘CSE’
• What is the total salary paid by rank in
each dept
• select dept ,rank,sum(salary)
• from faculty
• where group by dept,rank

• what is the total salary paid to the faculty ?
Select sum(salary)
from faculty
Select col-1 [,col-2]…
[GROUP BY col-1 [, col-2]…..]
[HAVING search-condition]
• Which departments have an average salary
above Rs:65000.Order the results by
average salary in the descending order of
salary
• Select dept,avg(salary)
from faculty
group by dept
having avg(salary)> 65000
Order by avg(salary) desc
NON-QUALIFIED JOIN
• A1 B1
A2 B2

A3 B3

A4 B4
• Select *
A1 B1
from A,B
. .
. .
. .
JOIN
• Select col-1[,col2]
FROM table-1,table-2 [,table3]…..
WHERE table-1.col-name=table2.col-
name [,AND table2.col-name=table-3.col-
name]…..
• Display the list of courses and the name of
faculty member teaching course

• Select crsnbr,cname,fname
from faculty,course
where faculty.fid= course.fid
• In which course is the student named
Fagin enrolled

• Select crsnbr
from student crsenrl
where sname=‘Fagin’
and student.sid=crsenrl.sid
• Prvide a class roster of students enrolled in
CS625 . The report should include course
no,course name and the student name

• Select course.crsnbr,cname,sname
from course ,crsenrl, student
where course.crsnbr=crsenrl.crsnbr and
crsenrl.sid=student.sid
NESTING QUERIES
SELECT col-1[,col-2] … FROM
table-name
WHERE col-name IN
(SELECT col-name
FROM table-name
WHERE search condition)
1. subqueries single-col
2. 16 level
• List the names of all students enrolled in cs625

. Select sname
from student
where sid in
( select sid from crsenrl
Where crsnbr =‘cs625’)
• List the name any student enrolled in
introduction to database system, who received
an ‘U’ in the course

• Select sname
from student
where sid in
(select sid
from crsenrl
where grade=‘U’
and crsnbr=(select crsnbr from course
where cname=‘introduction to data’) )
• List the names of the faculty members in
the same dept as PETER’S dept

select fname,dept
from faculty
where dept=(select dept
from faculty
where fname=‘PETER’)
SELF JOIN
• Alias
FROM table-name[alias1][,table-name [alias2]….
. List the names of any Assistant professors who are
earning more than any professors
select asst.fname,asst.salary
from faculty asst, faculty prof
where asst.rank= ‘A.P’and prof.rank=‘p’
and asst.salary>prof.salary
• Dispay the number of all students with majors and non-
majors taking courses.
Select sid
from student
where major=‘case’
union
select distinct sid
from crsenrl
where crsnbr like’cs%’
order by 1
• INSERTING MULTIPLE ROWS BY
COPYING FROM OTHER TABLES

INSERT INTO table-name


SELECT expression1[, expression2]…
FROM table-name
[WHERE search-condition]
THE UNION OPERATOR
• SELECT stmt
UNION
SELECT stmt
UNION
SELECT stmt

[ ORDER BY integer [desc]]


DELETE COMMAND
• DELETE FROM table-name
[WHERE search-condition]
EXAMPLE
delete sid=106 from cs650
delete from crsenrl
where crsnbr=‘cs650’ and sid=‘106’
UPDATING RECORDS
• UPDATE table-name
set column-name1=expression1
[,column-name2=expression2]
[WHERE search-condition]
EXAMPLE
change the grade of sid10 from “b” in cse14
update crsenrl
set grade=‘A’
where sid=’10’and crsnbr=‘cse’
VIEWS
• CREATE VIEW view-name [(view-
coloumn-name[,view-column-name-2]…)]
• AS SELECT expression1[,expression2]…
• FROM table-name-1[,table-name-2]...
[WHERE search-condition]
[GROUP BY column-name-1 [,coloumn-
name-2]
having search-condition]
• view column names must be specified for all
columns in a view if any column within the view
is based on a aggregate function(SUM,AVG,etc)
or an arithmetic calculation
• all variations of a SELECT statement can be
used to define a view.the only restriction is that
the ORDERBY clause is not permitted when
defining a view.
• create a view for cs faculty with fields
fid,fname and ext
• create a view cs-fac
as select fid,fname,ext
from faculty
where dept=‘cse’
query on this
select fname, ext
from cs-fac
order by fname
• TRY IT YOURSELF:
• create a view called roaster with fields
course number , course name,fname,sid
and sname
• UPDATE,INSERT AND DELETE
ARE POSSIBLE USING VIEWS.

• DROPPING A VIEW:
• DROP VIEW view- name
• example:
• drop view cs-fac
SECURITY
• Granting privileges
• SELECT
• INSERT
• UPDATE
• DELETE
• ALTER
 GRANT [ALL] [PREVILAGE1
[,PREVILAGE2]…

ON TABLE-NAME-1 TABLE-NAME-2
VIEW-NAME-1 , VIEW-NAME-2 …
 to PUBLIC
userid1[,userid2]…...

[ WITH GRANT OPTION ]


• grant select
on faculty
to dear
• grant update,insert
on faculty
to clerk1,clerk2
• grant all
on faculty
on registrar
with grant option
• grant select
on course
to public
on views also
grant can be done
 REVOKE [ALL] [PREVILAGE1
[,PREVILAGE2]…

ON TABLE-NAME-1 TABLE-NAME-2
VIEW-NAME-1 , VIEW-NAME-2 …
 FROM PUBLIC
userid1[,userid2]…...
example: REVOKE update, insert on faculty from
clerk1,clerk2.
ALTER TABLE
• ALTER TABLE table-name
ADD col-name data –type
• ALTER table student
add fid char(3)
INDEXES
• CREATE [UNIQUE]INDEX index-name
ON table-name
(col-name-1[des] [,col-name2[desc]]…]
create index majorind
on student (major)

Drop index index-name

You might also like