Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Lectuer-6

Database Design and


Applications
BITS Pilani Ashish Narang
Pilani Campus
BITS Pilani
Pilani Campus

First Semester

2020-21
Contents

• Join Operation in SQL


• Use of Aliases
• Outer Joins
• Aggregate Functions
• Use of Group by and Having
• Union operation in SQL
• Nested Queries
• Correlated Nested Queries
• Views

BITS Pilani, Pilani Campus


Join Query

Query: Retrieve the name and Address of all employees who


work for the 'Research' department.

SELECT Fname, Lname, Address


FROM EMP, DEPT
WHERE Dname='Research' AND Dnumber=Dno

BITS Pilani, Pilani Campus


ALIASES

Query: Retrieve the name and Address of all employees who


work for the 'Research' department.

SELECT E.Fname, E.Lname, E.Address


FROM EMPLOYEEE E, DEPARTMENT D
WHERE D.Dname='Research' AND
D.Dnumber=E.Dumber

BITS Pilani, Pilani Campus


Contd..

Query: Retrieve fname, lname and dname of all employees who


works for department number 10 or 20 or 30.

SELECT Fname, Lname


FROM EMPLOYEE E, DEPARTMENT D
WHERE E.Dno=D.Dnumber AND Dno
IN(10,20,30);

BITS Pilani, Pilani Campus


Outer Join

• An outer join extends the result of a simple join. An outer join


returns all rows that satisfy the join condition and also returns
some or all of those rows from one table for which no rows
from the other satisfy the join condition. There are 3 variants
of outer join.
1. Left outer join
2. Right outer join
3. Full outer join

BITS Pilani, Pilani Campus


Left Join

• Left join returns all the rows from left table combine with the
matching rows of the right table. If you get no matching tuples
in the right table it returns NULL values.

Select fname, lname, dname


from employee e left join department d
on e.dno=d.dno;

BITS Pilani, Pilani Campus


Right Join

• Right join returns all the rows from right table combine with
the matching rows of the left table. If you get no matching in
the left table it returns NULL values.

Select fname, lname, dname

From Employee e right join Department d

on e.dno=d.dno;

BITS Pilani, Pilani Campus


Use of Arithmetic in Queries

Query: Show the resulting salaries if every employee


working in Research Department is given a 10 percent raise.

SELECT E.Fname, E.Lname, 1.1 * E.Salary AS Increased_sal


FROM EMPLOYEE E, Department D
WHERE E.Dno=D.Number AND D.Dnumber=‘Research’;

BITS Pilani, Pilani Campus


Aggregate Functions

Query: Find the sum of the salaries of all employees, the maximum
salary, the minimum salary, and the average salary.
SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)
FROM EMPLOYEE;

Query: Find the sum of the salaries of all employees of the ‘Research’
department, as well as the maximum salary, the minimum salary,
and the average salary in this department.
SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)
FROM (EMPLOYEE JOIN DEPARTMENT ON Dno=Dnumber)
WHERE Dname=‘Research’;

BITS Pilani, Pilani Campus


Contd..

Query: Retrieve the total number of employees in the


company and the number of employees in the
‘Research’ department.

SELECT COUNT (*)


FROM EMPLOYEE;

SELECT COUNT (*)


FROM EMPLOYEE E, DEPARTMENT D
WHERE E.DNO=D.DNUMBER AND D.DNAME=‘Research’;

BITS Pilani, Pilani Campus


Use of Group By

Query: For each department, retrieve the department number, the


number of employees in the department, and their average
salary.

SELECT Dno, COUNT (*), AVG (Salary)


FROM EMPLOYEE
GROUP BY Dno;

BITS Pilani, Pilani Campus


Use of Having

Query: For each department where number of employees are


greater than 2, retrieve the department number, the number of
employees in the department, and their average salary.

SELECT Dno, COUNT (*), AVG (Salary)


FROM EMPLOYEE
GROUP BY Dno;
Having count(*) >=2

BITS Pilani, Pilani Campus


Union Operation
Query: Make a list of all project numbers for projects that involve an employee
whose last name is ‘Smith’, either as a worker or as a manager of the
department that controls the project.

SELECT DISTINCT Pnumber


FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn
AND Lname='smith'
UNION

SELECT DISTINCT Pnumber


FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE Pnumber=Pno AND Essn=Ssn
AND Lname='smith'

BITS Pilani, Pilani Campus


Use of Join with Group By

Query: For each project on which more than two employees


work, retrieve the project number, project name, and the
number of employees who work on that project.

SELECT Pnumber, Pname, COUNT(*)


FROM PROJECT, WORKS_ON
WHERE Pnumber=Pno
GROUP BY Pnumber, Pname
HAVING COUNT (*) > 2 ;

BITS Pilani, Pilani Campus


Contd..

Query: Retrieve a list of employees and the projects they are


working on, ordered by department and, within each
department, ordered alphabetically by last name, then first
name.
SELECT D.Dname, E.Lname, E.Fname, P.Pname
FROM DEPARTMENT D, EMPLOYEE E, WORKS_ON
W, PROJECT P
WHERE D.Dnumber= E.Dno AND E.Ssn= W.Essn AND
W.Pno= P.Pnumber
ORDER BY D.Dname, E.Lname, E.Fname;

BITS Pilani, Pilani Campus


Nested Queries

Query: Retrieve the name and Address of all employees who


work for the 'Research‘ department.

SELECT Fname, Lname, Address


FROM Employee
WHERE Dno IN
(
SELECT Dnumber FROM Department
WHERE Dname='Research'
);

BITS Pilani, Pilani Campus


Correlated Nested Query

Query: Retrieve the name of each employee who has a dependent


with the same first name as the employee.

SELECT E.Fname, E.Lname


FROM EMPLOYEE E
WHERE E.Ssn IN
(
SELECT Essn FROM DEPENDENT
WHERE Essn=E.Ssn AND
E.Fname=Dependent_name
);

BITS Pilani, Pilani Campus


Views

• A view is a subset of a database that is generated from a query


and stored as a permanent object.
• The data contained therein is dynamic depending on the point
in time at which the view is accessed.
• They take up very little storage space because the database
contains only the view definition, not the data
• Views can limit the degree to which tables are exposed to the
outer world.

BITS Pilani, Pilani Campus


Contd..
The syntax of create view is as follows:
CREATE VIEW "VIEW_NAME" AS "SQL Statement";

Example 1:
CREATE VIEW EMP_DETAILS
AS SELECT FNAME,LNAME FROM EMLOYEE

Example 2:
CREATE VIEW WORKS_ON1
AS SELECT Fname, Lname, Pname, working_hours
FROM EMPLOYEE, PROJECT, WORKS_ON
WHERE Ssn=Essn AND Pnum=Pnumber;

BITS Pilani, Pilani Campus

You might also like