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

SQL- Practice Problem1

The document outlines a practice problem for a Database Systems course at the National University of Computer and Emerging Sciences, Lahore Campus. It includes a database schema with tables for employees, departments, and salary grades, along with a series of SQL exercises that require writing queries for various data retrieval tasks. The exercises cover topics such as basic SQL queries, functions, group functions, joins, and subqueries.

Uploaded by

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

SQL- Practice Problem1

The document outlines a practice problem for a Database Systems course at the National University of Computer and Emerging Sciences, Lahore Campus. It includes a database schema with tables for employees, departments, and salary grades, along with a series of SQL exercises that require writing queries for various data retrieval tasks. The exercises cover topics such as basic SQL queries, functions, group functions, joins, and subqueries.

Uploaded by

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

National University of Computer and Emerging Sciences, Lahore Campus

Course: Database Systems


Program: BS(Computer Science)
Instructor: Muhammad Ishaq Raza

Practice Problem: SQL (1)

Conside the following database:

EMP (Empno, Ename, Job, Mgr, Hiredate, Sal, Comm, Deptno)


DEPT (Deptno, Dname, Loc)
SALGRADE (Grade, LoSal, HiSal)

EMP
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 13-JUN-83 800 20


7499 ALLEN SALESMAN 7698 15-AUG-83 1600 300 30
7521 WARD SALESMAN 7698 26-MAR-84 1250 50 30
7566 JONES MANAGER 7839 31-OCT-83 2975 20
7654 MARTIN SALESMAN 7698 05-DEC-83 1250 1400 30
7698 BLAKE MANAGER 7839 11-JUN-84 2850 30
7782 CLARK MANAGER 7839 14-MAY-84 2450 10
7788 SCOTT ANALYST 7566 05-MAR-84 3000 20
7839 KING PRESIDENT 09-JUL-84 5000 10
7814 TURNER SALESMAN 7698 04-JUN-84 1500 0 30
7876 ADAMS CLERK 7788 04-JUN-84 1100 20
7900 JAMES CLERK 7698 23-JUL-84 950 30
7902 FORD ANALYST 7566 05-DEC-83 3000 20
7934 MILLER CLERK 7782 21-NOV-83 1300 10

DEPT SALGRADE

DEPTNO DNAME LOC GRADE LOSAL HISAL

10 ACCOUNTING NEW YORK 1 700 1200


20 RESEARCH DALLAS 2 1201 1400
30 SALES CHICAGO 3 1401 2000
40 OPERATIONS BOSTON 4 2001 3000
5 3001 9999
Write down SQL Queries for each of the following statement:

Exercise 1 – Introduction to SQL

1. Select all information from SALGRADE table.


2. Select all information from EMP table.
3. List all employees who have a salary between 1000 and 2000.
4. List department numbers and names in department name order.
5. Display all the different job types.
6. List the details of the employees in departments 10 and 20 in alphabetical order of names.
7. List names and jobs of all clerks in department 20.
8. Display all employees names which have TH or LL in them.
9. List details for all employees who have a manager.
10. Display names and total remuneration for all employees.
11. Display all employees who were hired during 1983.
12. Display name, annual salary and commission of all salespeople whose monthly salary is greater than their
commission. The output should be ordered by salary, in descending order.
13. Display all employee information in ascending order of manager number. King must be last.

Exercise 2 – Using Functions

This exercise covers functions not just in the SELECT but in WHERE and ORDER BY clauses. Note the column alias
which have been used.

1. List the employee name and salary increased by 15% and expressed as a whole number of dollars.
2. Display each employee’s name and hiredate from dept. 20.
3. Display each employee name with hiredate, and salary review date. Assume review date is one year after hiredate.
Order the output in ascending review date order.
4. Print a list of employees displaying just the salary amount if more than 1500. If exactly 1500 display ‘On Target’,
if less than 1500 display ‘Below 1500’.
5. Write a query which will return the DAY of the week (i.e. MONDAY,) of hiredate of employees.
6. Employees hired on or before the 15th of any month are paid on the last Friday of that month. Those hired after
the 15th are paid on the last Friday of the following month. Print a list of employees, their hiredate and first pay
date. Sort on hiredate.
7. Write a query to calculate the length of time any employee has been with the company.

Exercise 3 – Group Functions

1. Find the minimum salary of all employees.


2. Find the minimum, maximum and average salaries of all employees.
3. List the minimum and maximum salary for each job type.
4. Find out how many managers there are without listing them.
5. Find the average salary and average total remuneration for each job type. Remember salesmen earn commission.
6. Find out the difference between highest and lowest salaries.
7. Find all departments which have more than 3 employees.
8. Check whether all employee numbers are indeed unique.
9. List lowest paid employees working for each manager. Exclude any groups where the minimum salary is less
than 1000. Sort the output by salary.
Exercise 4 – Joins

1. Display all employee names and their department name, in department name order.
2. Display all employee names, department number and name.
3. Display the name, location and department of employees whose salary is more than 1500 a month.
4. Produce a list showing employees’ salary grades.
5. Show only employees on grade 3.
6. Show all employees in Dallas.
7. List the employee name, job salary, grade and department name for everyone in the company except clerks. Sort
on salary, displaying the highest salary first.
8. Display the department that has no employees.
9. List all employees by name and number along with their manager’s name and number.
10. Modify solution to question 10 to display KING who has no manager.
11. Find the job that was filled in the first half of 1983, and the same job that was filled during the same period in
1984.
12. Find all employees who joined the company before their manager.

Exercise 5 – Subqueries

1. Write a query to display the employee name and hiredate for all employees in the same department as Blake.
Exclude Blake.
2. Create a query to display the employee number and name for all employees who earn more than the average
salary. Sort the results in descending order of salary.
3. Write a query that will display employee number and name for all employees who work in a department with any
employee whose name contains a T.
4. Display the employee name, department number, and job title for all employees whose department location is
Dallas.
5. Display the employee and salary of all employees who report to King.
6. Display the department number, name, and job for all employees in the Sales department.
7. Write a query to display the name, department number, and salary of any employee whose department number
and salary match the department number and salary of any employee who earns a commission.
8. Display the name, department name, and salary of any employee whose salary and commission match the salary
and commission of any employee located in Dallas.
9. Create a query to display the name, hiredate, and salary for all employees who have the same salary and
commission as Scott.
10. Create a query to display the employees that earn a salary that is higher than the salary of all of the clerks. Sort
the results on salary from highest to lowest.

You might also like