Manual No 3
Manual No 3
Chapter 3
Objectives
Introduction and practise of DRL
Introduction
WHERE
WHERE specifies which rows to retrieve.
A WHERE clause in SQL specifies that a SQL Data Manipulation Language (DML) statement
should only affect rows that meet a specified criteria. WHERE clauses are not mandatory
clauses of SQL DML statements, but should be used to limit the number of rows affected by
a SQL DML statement or returned by a query. WHERE is an SQL reserved word.
EXAMPLE : SELECT * FROM student WHERE mark > 90
GROUP BY
GROUP BY groups rows sharing a property so that an aggregate function can be applied to
each group.
A GROUP BY statement in SQL specifies that a SQL SELECT statement returns a list that is
grouped by one or more columns, usually in order to apply some sort of aggregate function
to certain columns.
SELECT branchno, AVG(mark) FROM student GROUP BY branchno;
HAVING
HAVING selects among the groups defined by the GROUP BY clause.
A HAVING statement in SQL specifies that a SQL SELECT statement should only return
rows where aggregate values meet the specified conditions.
Example:
SELECT branchno, AVG(mark) FROM student GROUP BY branchno HAVING AVG(mark)
Lab 3: DRL(DATA RETRIEVAL
> 80;
ORDER BY
ORDER BY specifies an order in which to return the rows.
An ORDER BY clause in SQL specifies that a SQL SELECT statement returns a result set with
the rows being sorted by the values of one or more columns. ORDER BY is the only way to
sort the rows in the result set. Without this clause, the relational database system may return
the rows in any order.
Example:
SELECT * FROM student ORDER BY registerno;
SELECT * FROM student ORDER BY mark;
7369 3200
7566 11900
7788 12000
7876 4400
UNION
This query is used to display the combined rows of two different queries, which are having
the same structure, without duplicate rows.
Syntax SELECT field _1,field _2,......FROM relation1 WHERE (Condition) UNION SELECT
field _1,field _2,........FROM relation2 WHERE (Condition); Example SQL> SELECT * FROM
STUDENT;
SNO SNAME
1 kumar
2 ravi
3 ramu
3 ramu
5 lalitha
9 devi
1 kumar
1 kumar
2 ravi
3 ramu
5 lalitha
INTERSET
This query is used to display the common rows of two different queries, which are having
the same structure, and to display a selected set of fields out of them. Syntax SELECT field
_1,field _2,.. FROM relation1 WHERE (Condition) INTERSECT SELECT field _1,field _2,..
FROM relation2 WHERE(Condition); Example SQL> SELECT * FROM student INTERSECT
SELECT * FROM std;
SNO SNAME
1 Kumar
MINUS
This query is used to display all the rows in relation1,which are not having in the relation2.
Syntax: SELECT field _1,field _2,......FROM relation1 WHERE(Condition) MINUS SELECT
field _1,field _2,..... FROM relation _2 WHERE(Conditon); SQL> SELECT * FROM student
MINUS SELECT * FROM std;
SNO SNAME
2 RAVI
3 RAMU
Lab 3: DRL(DATA RETRIEVAL
Summary
1- Create and insert random values in table named ’Student’ Student( name, registration
No., CNIC, DOB, Class, Department No, Class incharge, monthly Fee)
Note: table must have following departments CS,IT,Engineering,Arts
2- Create and insert values in table named ’Department’ Department( Dept No, Dept Name,
total students, total courses)
3- Perform following queries on above two tables.
• Display the Name and registration of students whose fees is greater than Rs.500
USE LAB3
CREATE TABLE Student( SName varchar(50), regNo varchar(50),
CNIC NUMERIC, DOB DATE, Class VARCHAR(50), DeptNo NUMERIC, Cls_Incharge VARCHAR(50), monFee
NUMERIC)
INSERT INTO Student(SName, regNo,
CNIC, DOB, Class, DeptNo, Cls_Incharge, monFee) VALUES
('Usman', 'swen22110104', '12345', '1-1-2004','Software',1,Ali,12000),
('Usman22', 'swen221101022', '123456', '1-1-2004','Software',2,Haji,10000),
('Usman44', 'swen221101044', '12345', '1-1s-2004','Software',3,Ahmar,20000),
('Usman36', 'swen221101036', '12345', '1-1-2004','Software',4,Qazi,15000)
CREATE TABLE Department( DeptNo NUMERIC, DeptName VARCHAR(50),
totalStudents NUMERIC, totalCourses NUMERIC)
INSERT INTO Department (DeptNo, DeptName,
totalStudents, totalCourses) VALUES (1, 'CS', 100, 12), (2 ,'IT', 90,8) , (3, 'Engineering',
112, 9), (4, 'Arts', 114,7)
-- • Show all items of ’Student’ table.
--• Show all items of ’Department’ table.
SELECT * FROM Student;
SELECT * FROM Department;
--Display the details of students who are in CS Department.
SELECT * FROM Student where DeptNo = 1;
--Display the name of the Department No of IT Department.
SELECT DeptNo FROM Department where DeptName = 'IT';
--Display the Number of students in each department.
SELECT totalStudents, DeptName FROM Department ;
--• Display the Name and registration of students whose fees is greater than Rs.500
SELECT sName, regNo FROM STUDENT where monFee>500;
--• Display the Maximum fees that a student pay.
SELECT max(monFee) AS maximumFee FROM Student;
Lab 3: DRL(DATA RETRIEVAL
Practise Exercise
1- Create and insert values in the table named ’Project’
• Display the project name and project manager whoes budget is lowest
• Display the project name and project manager whoes budget is highest
• Arrange the table in ascending order according to the project start date.
use Lab3
create table PROJECT (pno numeric(3) Primary Key,
pname varchar(60),
pmgr numeric(4) Not Null,
persons numeric(3),
budjet numeric(8,2),
pstart date Not Null,
pend date
)
INSERT INTO PROJECT (pno, pname, pmgr, persons, budjet, pstart, pend) VALUES
(1, 'PROJECT1', 1 ,5, 10000, '1-1-2011','1-1-2012'),
(2, 'PROJECT2', 2 ,3, 9000, '1-1-2013','1-1-2014'),
(3, 'PROJECT3', 3 ,5, 19000, '1-1-2014','1-1-2015'),
(4, 'PROJECT4', 4 ,4, 13000, '1-1-2015','1-1-2016'),
(5, 'PROJECT5', 5 ,7, 15000, '1-1-2016','1-1-2017')
-- Display all items of table
SELECT * FROM PROJECT;
--• Display only Project name and Budjet of every project
SELECT pname, budjet FROM PROJECT;
--• Display the project name and project manager whoes budget is lowest
SELECT pname, pmgr FROM PROJECT WHERE budjet =(SELECT min(budjet) FROM PROJECT);
--• Display the project name and project manager whoes budget is highest
SELECT pname, pmgr FROM PROJECT WHERE budjet =(SELECT max(budjet) FROM PROJECT);
--• Arrange the table in ascending order according to the project start date.
SELECT * FROM PROJECT ORDER BY pstart ASC;
--• Display only those manager whose persons are more than 5.
SELECT pmgr as Project_Manager FROM PROJECT where persons>5;
--• Delete the record of those manager whoes budget is the lowest.
DELETE FROM PROJECT WHERE budjet = (SELECT MIN(budjet) FROM PROJECT);
SELECT * FROM PROJECT;
--• Update the Datatype of Project name to varchar2(30)*/
ALTER TABLE PROJECT
ALTER COLUMN pname VARCHAR(30);
Lab 3: DRL(DATA RETRIEVAL