SELECT Last - Name, Job - Id FROM Employees WHERE Job - Id (SELECT Job - Id FROM Employees WHERE Employee - Id 141)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

DEPARTMENT OF COMPUTER SCIENCE

( Rachna College of Engineering and Technology Gujranwala )

DATABASE SYSTEMS

Name: Registration No:

LAB 08

Using Subqueries to Solve Queries and Using the Set Operators

Using a Subquery to Solve a Problem


“Which employee has salary greater then salary of Abel's salary ?”
To solve this problem, you need two queries: one to find how much Abel earns, and a second
query to find who earns more than that amount. You can solve this problem by combining the
two queries, placing one query inside the other query. he inner query (or sub query) returns a
value that is used by the outer query (or main query). Using a sub query is equivalent to
performing two sequential queries and using the result of the first query as the search value in the
second query. The inner query (or sub query) returns a value that is used by the outer query (or
main query). Using a sub query is equivalent to performing two sequential queries and using the
result of the first query as the search value in the second query.

You can place the sub query in a number of SQL clauses, including the following:
WHERE, FROM and HAVING
Types of Subqueries
Single-row Subqueries: Queries that return only one row from the inner SELECT statement.
Multiple-row Subqueries: Queries that return more than one row from the inner SELECT
statement. If sub query returns multiple rows then we will use IN, ALL and ANY operators.
Single-row Subqueries Examples:
1- Display the employees whose job ID is the same as that of employee 141:
SELECT last_name, job_id FROM employees WHERE job_id = (SELECT job_id FROM employees
WHERE employee_id = 141);
2- Displays the employee last name, job ID, and salary of all employees whose salary is equal to
the minimum salary.
3- Find the job with the lowest average salary.
SELECT job_id, AVG(salary) FROM employees GROUP BY job_id HAVING AVG(salary)=(SELECT
MIN(AVG(salary)) FROM employees GROUP BY job_id);

Multiple-row Subqueries Examples:


1- Find the employees who earn the same salary as the minimum salary for each department.
SELECT last_name, salary, department_id FROM employees WHERE salary IN (SELECT MIN(salary)
FROM employees GROUP BY department_id);
2- Using the ANY Operator:
List the employees who are not IT programmers and whose salary is less than that of any IT
programmer.

<ANY means less than the maximum. >ANY means more than the minimum. =ANY is
equivalent to IN.
3- Using the ALL Operator:

>ALL means more than the maximum, and <ALL means less than the minimum.

You can use sub query in FROM clause.


Select first_name from (Select * from employees where department_id=20)

LAB TASKS
1- The HR department needs a query that displays the last name and hire date of any employee in
the same department as the employee whose name is Zlotkey(excluding Zlotkey) i.e. find all
employees who work with Zlotkey (excluding Zlotkey).

2- Create a report that displays 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.
3- Write a query that displays the employee number and last name of all employees who work in
a department with any employee whose last name contains a u.

4- The HR department needs a report that displays the last name, department number, and job ID
of all employees whose department location ID is 1700.

5- Create a report for HR that displays the last name and salary of every employee who reports to
King.

6- Create a report for HR that displays the department number, last name, and job ID for every
employee in the Executive department.

7- display the employee number, last name, and salary of all employees who earn more than the
average salary and who work in a department with any employee whose last name contains a u.

Using the Set Operators


The set operators combine the results of two or more component queries into one result. Queries
containing set operators are called compound queries.

Examples:
Display the current and previous job details of all employees. Display each employee only once.
Display the employee IDs and job IDs of those employees who currently have a job title that is
the same as their job title when they were initially hired (that is, they changed jobs but have now
gone back to doing their original job).

Display the employee IDs of those employees who have not changed their jobs even once.

LAB TASKS
1- The HR department needs a list of department IDs for departments that do not contain the job
ID ST_CLERK. Use set operators to create this report.

2- The HR department needs a list of countries that have no departments located in them.
Display the country ID and the name of the countries. Use set operators to create this report.

3- Produce a list of jobs for departments 10, 50, and 20, in that order. Display job ID and
department ID using set operators.

4- Create a report that lists the employee IDs and job IDs of those employees who currently have
a job title that is the same as their job title when they were initially hired by the company (that is,
they changed jobs but have now gone back to doing their original job).

5- The HR department needs a report with the following specifications:


- Last name and department ID of all the employees from the EMPLOYEES table,
regardless of whether or not they belong to a department
- Department ID and department name of all the departments from the DEPARTMENTS
table, regardless of whether or not they have employees working in them
Write a compound query to accomplish this.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy