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

Database lab work

Uploaded by

arman64658852
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Database lab work

Uploaded by

arman64658852
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Course Title: Database Systems

Lab Task Number: 7

Submitted by: Huzaifa Safdar

Registration No: FA23-BCS-034

Submitted To: Dr. Mohsin Fayyaz

Section: BCS-3A
LAB TASK 1

Write a SQL statement that uses subquery to display the last name and hire date of any employee in the

same department as the employee whose last name is provided by the user For example, if the user enters

Zlotkey, find all employees who work with Zlotkey (excluding Zlotkey).

QUERY:

SELECT last_name,hire_date

FROM employees

WHERE department_id = (

SELECT department_id

FROM employees

WHERE last_name = 'Zlotkey'

AND last_name != 'Zlotkey';


OUTPUT:
LAB TASK 2

Write a SQL statement that uses subquery to display the employee number, last name, and salary of all

employees who earn more than the average salary. Sort the results in order of ascending salary..

QUERY:

SELECT employee_id, last_name, salary

FROM employees

WHERE salary > (

SELECT AVG(salary)

FROM employees

ORDER BY salary ASC;

OUTPUT:
Lab Task 3

Write a SQL statement that uses subquery to display the department number, last name, and job ID for

every employee in the Executive department.

QUERY:

SELECT department_id, last_name, job_id

FROM employees

WHERE department_id = (

SELECT department_id

FROM departments

WHERE department_name = 'Executive'

);

OUTPUT:
LAB TASK 4

Write a SQL statement that uses subquery to find those departments where maximum salary is 7000 and

above. The employees worked in those departments have already completed one or more jobs. Return all

the fields of the departments.

QUERY:

SELECT *

FROM departments

WHERE department_id IN (

SELECT department_id

FROM employees

WHERE salary >= 7000

AND job_id IS NOT NULL

GROUP BY department_id

HAVING MAX(salary) >= 7000

);

OUTPUT:
LAB TASK 5
Write a SQL statement that uses subquery to find those employees whose department is located in the city

'London'. Return first name, last name, salary, and department ID.

QUERY:

SELECT first_name, last_name, salary, department_id

FROM employees

WHERE department_id IN (

SELECT department_id

FROM departments

WHERE location_id = (

SELECT location_id

FROM locations

WHERE city = 'London'

);

OUTPUT:

LAB TASK 6

Write a SQL statement that uses subquery to display the last name and salary of every employee who
reports to King.

QUERY:

SELECT last_name, salary

FROM employees

WHERE manager_id = (

SELECT employee_id

FROM employees

WHERE last_name = 'King'

);

OUTPUT:

You might also like