SQL-AdvancedSQL-AdvancedSQL-AdvancedSQL-Advanced
SQL-AdvancedSQL-AdvancedSQL-AdvancedSQL-Advanced
CASCADE option removes whatever other references are there for the column netpay
RESTRICT option will not removes if other references are there for the column netpay
4. Removing Table
DROP TABLE EMP CASCADE;
Exercise VII
(Aggregate functions COUNT,SUM,MAX,MIN,AVG)
Aggregate functions are used to summarize information from multiple tuples into a
single-tuple summary.
1. 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 EMP;
2. We could use AS to rename(using alias) the column names in the resulting single-row table
SELECT SUM (Salary) AS Total Sal, MAX (Salary) AS Highest Sal, MIN(Salary) AS
Lowest Sal, AVG (Salary) AS Average Sal FROM EMP;
Exercise VIII
Date Manipulations
Date Functions.
3. Date arithmetic.
D = date1 – date2 to find the difference between two dates
Date1 = date1+10 – to add 10 days to date1
Exercise: Create a Library database with the following information: Access number, customer
number title, author, publisher, issue date, due date, fine, returned date, number of days.
We want to apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are
based on some attribute values.
SELECT Dno, COUNT (*), AVG (Salary) FROM EMPLOYEE GROUP BY Dno;
2. Sometimes we want to retrieve the values of these functions only for groups that
satisfy certain conditions on aggregate values – we use HAVING option
To find the number and average salary of employees in each department where number of
employees more than 5
SELECT Dno, COUNT (*), AVG (Salary) FROM EMPLOYEE GROUP BY Dno
HAVING COUNT (*) >5;
I. Create a sales table containing date of sales, item no of sales (A, B, C, D), item type (Cosmetic,
Textile, Toys), salesman no., amount of sales.
1. Add 10 records.
2. Find the grand total sales.
3. Find the salesman wise total amount.
4. Find area wise total sales.
5. Find area wise total sales for cosmetic items only.
6. Find salesman wise total for area A only.
7. Find salesman wise total for a particular area B and for item textile only.
8. To find total based on area wise, and within area item wise.
9. To find area wise total and with each area salesman wise and within each salesman item wise.
10. Repeat 9 for a particular date only.
Exercise IX
Nested Query/Sub Query ,Table alias, Rename of attributes and Self Join (Joining
table into itself)
1. To display the employee names who are working in the same department of Mr.John
SELECT NAME FROM EMP WHERE DNO=(SELECT DNO FROM EMP WHERE
NAME =’John’);
2. To display the employee names who are working in the same departments of Mr.John or Mr
Mathew
SELECT NAME FROM EMP WHERE DNO IN (SELECT DNO FROM EMP WHERE
NAME =’John’ OR NAME = ‘Mathew’ );
3. To display the employee names who are having salary more than the salary of Mr John
SELECT NAME FROM EMP WHERE salary > (SELECT salary FROM EMP WHERE
NAME =’John’)
4. To display the employee names who are having salary more than the all salary of Mr John or Mr
Mathew
SELECT NAME FROM EMP WHERE salary > ALL (SELECT salary FROM EMP
WHERE NAME =’John’ OR NAME = “Mathew’);
5. To display the employee names who are having salary more than the any of salary of Mr John or
Mr Mathew
SELECT NAME FROM EMP WHERE salary > ANY (SELECT salary FROM EMP
WHERE NAME =’John’ OR NAME = “Mathew’);
6. To display the employee names who are having salary more than the all salary of Mr John or Mr
Mathew
SELECT NAME FROM EMP WHERE salary > ALL (SELECT salary FROM EMP
WHERE NAME =’John’ OR NAME = “Mathew’);
7. To display the employee names who are working in the same dept and designation of Mr John
or Mr Mathew works
SELECT NAME FROM EMP WHERE (dept,desig) IN (SELECT dept,desig FROM
EMP WHERE NAME =’John’ OR NAME = “Mathew’);
8. The EXISTS function in SQL is used to check whether the result of a nested query is empty
(contains no tuples) or not
To return true or false if dept,desig exists in the list
SELECT NAME FROM EMP WHERE (dept,desig) EXISTS (SELECT * FROM EMP
WHERE NAME =’John’ OR NAME = “Mathew’);
SELECT NAME FROM EMP WHERE (dept,desig) NOT EXISTS (SELECT * FROM
EMP WHERE NAME =’John’ OR NAME = “Mathew’);
Exercise
1. Add a column SUPERVISOR number in EMPLOYEE table.
2. Add data into the new column by taking values from empno column.
3. Using self join list the employee name with their supervisor name with treating of column as
Employee Name and Supervisor Name.
4. List the emp name and name of head by joining EMPLOYEE and DEPARTMENT using
JOIN..ON command.
5. Implement the above query using NATURAL JOIN (Refer page 268).
6. Make same SUPERVISOR number in EMPLOYEE as NULL value and using LEFT
OUTER JOIN to include all employee name in question no. 1.(Refer page 268)
7. Using JOIN..ON combine PROJECT, DEPARTMENT, EMLPOYEE to get employee name,
project name and department name.
8. Implement the above question no. 6 using simple joining of tables (by listing the tables
operated by comma)
Exercise X
Set operations (UNION, INTRESECTION,MINUS)
Create three tables based on students interested in ‘football’, ‘basket ball’ and ‘cricket’ each table
containing same columns such as class number, name, course name.
Add 5 records in each table including some common records.
1. Make the student list interested in foot ball or basket ball.
2. Students interested either in both foot, basket ball or in cricket.
3. Students interested in both foot ball and cricket.
4. Students interested in all games.
5. those interested in foot ball only.
6. Interested in foot ball and basket ball and in cricket.
Exercise XI
Managing Views
A view in SQL terminology is a single table that is derived from other tables. These other tables can be
base tables or previously defined views. A view does not necessarily exist in physical form; it is
considered to be a virtual table, in contrast to base tables, whose tuples are always physically stored in
the database. This limits the possible update operations that can be applied to views, but it does not
provide any limitations on querying a view.
1. To create a view named ‘EVIEW’containing only empno and name derived from EMP
CREATE VIEW EVIEW AS SLECT EMPNO,NAME FROM EMP;
2. To create a view named ‘EVIEW’containing only empno,NAME, DNAME derived from EMP
AND DEPT TABLES
CREATE VIEW EVIEW AS SLECT EMPNO,NAME,DNAME FROM EM,DEPT WHERE
EMP.DNO=DEPT.DNO;
3. To create a view named ‘EVIEW’containing only empno and name of those having salary
greater than 50000 derived from EMP
CREATE VIEW EVIEW AS SLECT EMPNO,NAME FROM EMP WHERE SALARY
>50000;
4. A view can be managed just like a table for select, update et
SELECT * FROM EVIEW;
1.Create a View named as ‘EMP-SALES’ that contains name, designation and bpay of employees
in sales department only using the Employee table
2. List all rows available in the above view
1. List all employee details in the view ‘EMP-SALES’ having bpay >5000
2. Drop the above view
3. Create a view named ‘employee-department-project’ containing employee name, department
name, project name by selecting the four tables employee,department,project and assigned-to.
Take the column names as specified in the question
4. List all details available in the above view.
Exercise XII
Implementing Database Triggers
:
A trigger is a programme or instruction which will automatically execute when an event happens.
Example: Display a message “Out of stock” when the quantity in stock is less than the re order level
before update/insert operations.
The event(s): These are usually database update operations that are explicitly applied to the database. In
this example the events are: inserting a new employee record, changing an employee’s salary, or
changing an employee’s supervisor. The person who writes the trigger must make sure that all possible
events are accounted for. In some cases, it may be necessary to write more than one trigger to cover all
possible cases. These events are specified after the keyword BEFORE in our example, which means that
the trigger should be executed before the triggering operation is executed. An alternative is to use the
keyword AFTER, which specifies that the trigger should be executed after the operation specified in the
event is completed.
2. The condition that determines whether the rule action should be executed: Once the triggering event
has occurred, an optional condition may be evaluated. If no condition is specified, the action will be
executed once the event occurs. If a condition is specified, it is first evaluated, and only if it evaluates to
true will the rule action be executed. The condition is specified in the WHEN clause of the trigger.
3. The action to be taken: The action is usually a sequence of SQL statements, but it could also be a
database transaction or an external program that will be automatically executed. In this example, the
action is to execute the stored procedure INFORM_SUPERVISOR.
To write a trigger to display the message “Salary greater than maximum allowed” which has to be
executed on the event of before insert or update salary and when the new salary becomes more than
50000.
To write a trigger to add insert a new row in purchase order when item in stock exceed re-order level.
**************
************
**********
*******
****
**