0% found this document useful (0 votes)
3 views7 pages

SQL-AdvancedSQL-AdvancedSQL-AdvancedSQL-Advanced

The document outlines various SQL exercises related to schema evolution commands, aggregate functions, date manipulations, grouping of tuples, nested queries, set operations, managing views, and implementing database triggers. It includes specific SQL commands for altering tables, calculating employee salaries, creating views, and defining triggers for database events. Each exercise provides examples and instructions for performing operations on database tables and data manipulation.

Uploaded by

sou
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)
3 views7 pages

SQL-AdvancedSQL-AdvancedSQL-AdvancedSQL-Advanced

The document outlines various SQL exercises related to schema evolution commands, aggregate functions, date manipulations, grouping of tuples, nested queries, set operations, managing views, and implementing database triggers. It includes specific SQL commands for altering tables, calculating employee salaries, creating views, and defining triggers for database events. Each exercise provides examples and instructions for performing operations on database tables and data manipulation.

Uploaded by

sou
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/ 7

Exercise V

(Schema Evolution commands – ALTER, DROP)


Date of Submission :

1 To add a column netpay number(10,2);


ALTER TABLE EMP ADD COLUMN netpay NUMBER(10,2);

2. To drop a column netpay


ALTER TABLE EMP DROP COLUMN netpay NUMBER(10,2) CASCADE;

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

3. Adding and removing constraints in a table


ALTER TABLE EMP ALTER COLUMN bpay DROP DEFAULT;
ALTER TABLE EMP ALTER COLUMN bpay SET DEFAULT 5000;

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;

3. Retrieve the total number of employees in the company

SELECT COUNT (*) FROM EMPLOYEE;


4. Number of employees in the ‘Research’ department
SELECT COUNT (*) FROM EMPLOYEE, DEPARTMENT WHERE DNO = DNUMBER
AND DNAME = ‘Research’;

Exercise VIII
Date Manipulations
Date Functions.

1. To add a column type date, the keyword “DATE’ can be used

CREATE TABLE EMP (EMPNO NUMBER(10), BDATE DATE);


INSERT INTO EMP VALUES (101,’27-MAR-75’);

2. To_date(string,fmt) – To convert a character string to date format


Default format is ‘dd-MMM-yy’
Other formats are ‘DD/MM/YY’
‘Month DD,YYYY’
Select To_date(odate,’DD/MM/YY) from sales
- where odate is a character type data

3. To_char(d,fmt) - To convert a date to character type as format specified

Select to_char(odate,’DD/MM/YY) from sales


- Where odate is date type data

3. Date arithmetic.
D = date1 – date2 to find the difference between two dates
Date1 = date1+10 – to add 10 days to date1

SELECT BDATE+2 FROM EMP;

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.

1.Insert 5 records with Access number, Title, Author, Publisher.


2.Replace all issue date with today’s date(sysdate represents today’s date).
3. Set due date as issue date + 10days.
4.Assign some returned date including dates over due date.
5.Calculate number of days exceed after due date for books returned late.
6.Calculate fine with 25 Ps pair each exceeding date.
7.To replace the book title, customer number, issue date and due date(date in the format month-dd-yy).
8.Display all issue dates month (in alphabets).
9.Find the issue date 15 days after.
10.Find the number of days elapsed between today’s date and issue date.
Exercise IX
(Grouping of Tuples using GROUP BY)

We want to apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are
based on some attribute values.

1. To find the number and average salary of employees in each department

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)

Query within another query

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’);

9. DISTINCT – different values – DUPLICATION WILL BE AVOIDED

SELECT DISTINCT SALARY FROM EMP

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)

Joining two tables of same structure

Suppose EMP1, EMP2 are employees working two projects


1. List employees from either or both the projects
SELECT DISTICNT NAME FROM EMP1 UNION EMP2;
2. List employees IN project1 only
SELECT DISTICNT NAME FROM EMP1 MINUS EMP2;
3. List employees working in both the projects
4. SELECT DISTICNT NAME FROM EMP1 INTERSECT EMP2;

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.

CREATE TRIGGER SALARY_VIOLATION BEFORE INSERT OR UPDATE OF SALARY


ON EMP
FOR EACH ROW WHEN ( NEW.SALARY > 50000) DISPLAY “Salary Exceed maximum
Limit”

To write a trigger to add insert a new row in purchase order when item in stock exceed re-order level.

CREATE TRIGGER PORDER AFTER UPDATE OF QSTOCK ON STOCK


FOR EACH ROW WHEN ( NEW.STOCK > ROL) INSERT INTO PURCHASE VALUES
(ITNO,DESC,10);

**************
************
**********
*******
****
**

You might also like