0% found this document useful (0 votes)
21 views

Manual No 3

The document discusses the different clauses used in SQL such as WHERE, GROUP BY, HAVING, ORDER BY, JOIN, UNION, INTERSECT, MINUS. It also provides examples of queries using these clauses on sample tables. The last section provides an exercise on creating a PROJECT table and writing queries on it.

Uploaded by

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

Manual No 3

The document discusses the different clauses used in SQL such as WHERE, GROUP BY, HAVING, ORDER BY, JOIN, UNION, INTERSECT, MINUS. It also provides examples of queries using these clauses on sample tables. The last section provides an exercise on creating a PROJECT table and writing queries on it.

Uploaded by

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

Submitted By: SWEN221101044

Chapter 3

Lab 3: DRL(DATA RETRIEVAL


LANGUAGE)

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;

. JOIN using SELECT - FROM - ORDER BY


This query is used to display a set of fields from two relations by matching a common field
in them in an ordered manner based on some fields.
Syntax SELECT a set of fields from both relations FROM relation1, relation2 WHERE rela-
tion1.field _x = relation2.field _y ORDER BY field _z; Example SQL>SELECT empno,ename,job,dname
FROM emp,dept WHERE emp.deptno = 20 ORDER BY job;
EMPNO ENAME JOB DNAME

7788 SCOTT ANALYST ACCOUNTING


7902 FORD ANALYST ACCOUNTING

7566 JONES MANAGER OPERATIONS


7566 JONES MANAGER SALES
20 rows selected.

JOIN using SELECT - FROM - GROUP BY


This query is used to display a set of fields from two relations by matching a common field
in them and also group the corresponding records for each and every value of a specified
key(s) while displaying.
Syntax SELECT a set of fields from both relations FROM relation1,relation2 WHERE rela-
tion1.field _x=relation2.field _y GROUP BY field _z;
Example SQL> SELECT empno,SUM(SALARY) FROM emp,dept WHERE emp.deptno
=20 GROUP BY empno;
EMPNO SUM (SALARY)

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

SQL> SELECT * FROM STD;


SNO SNAME

3 ramu
5 lalitha
9 devi
1 kumar

SQL> SELECT * FROM student UNION SELECT * FROM std;


SNO SNAME

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.

• Show all items of ’Student’ table.

• Show all items of ’Department’ table.

• Display the details of students who are in CS Department.

• Display the name of the Department No of IT Department.

• Display the Number of students in each department.

• Display the Name and registration of students whose fees is greater than Rs.500

• Display the Maximum fees that a student pay.

• Display the minimum fees that a student pay.

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’

Data Field Name DataType Constraint


Project Number pno number(3) Primary Key
Project Name pname varchar2(60)
Project Manager pmgr number(4) Not Null
Persons persons number(3)
Budjet in Rupees budjet number(8,2)
Project Start date pstart date Not Null
Project End Date pend date
Perform the following queries on above table
’Project’ Write the Queries and show the results.

• Display all items of table

• Display only Project name and Budjet of every 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.

• Display only those manager whose persons are more than 5.

• Delete the record of those manager whoes budget is the lowest.

• Update the Datatype of Project name to varchar2(30)

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

You might also like