Professional Documents
Culture Documents
Lab1 SQL
Lab1 SQL
• 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]…..
• Stu1 Stu2
• Rollno Name Rollno Address
• 1 A 1 Chennai
• 2 B 2 Mumbai
• Select name, address
• From stu1, stu2
• Where stu1.rollno = stu2.rollno
• Stu1 Stu2
• rollno name rollno address
• 1 A 1 Chennai
• 2 B 2 Mumbai
• R1 = Stu1 x Stu2
• Stu1.rollno name stu2.rollno address
• 1 A 1 Chennai
• 1 A 2 Mumbai
• 2 B 1 Chennai
• 2 B 2 Mumbai
• Pick the rows from R1 using the selection
condition stu1.rollno = stu2.rollno
• Euqi-join (R2)
• stu1.rollno name stu2.rollno address
• 1 A 1 Chennai
• 2 B 2 Mumbai
• Select stu1.rollno, name, address from
R2
• Select crsnbr,cname,fname
from faculty,course
where faculty.fid= course.fid
• In which course is the student named Fagin enrolled.
Your answer must include student name, course number
and course name.
where sname=‘Fagin’
and student.sid=crsenrl.sid
and crsenrl.crnbr = course.crsnbr
• Prvide a class roster of students enrolled in CS625 . The
report should include course no,course name, the
student name and the name of faculty who is teaching
the course.
. 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
query on this
select fname, ext
from cs-fac
order by fname
• Faculty
• Fid fname dept CRSNBR EXT SAL
• 100 A CSE CSE004 3368 200000
• 200 B IT IT6234 3345 300000
• 300 C CSE Cs001 3358 150000
• 400 D MECH ME054 3399 350000
• 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]…...
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)