Professional Documents
Culture Documents
FALLSEM2021-22 CBS1007 ELA VL2021220104397 Reference Material I 06-Aug-2021 LAB1 SQL 1
FALLSEM2021-22 CBS1007 ELA VL2021220104397 Reference Material I 06-Aug-2021 LAB1 SQL 1
• 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.
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
• 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]…...
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)