0% found this document useful (0 votes)
22 views4 pages

Lab 08

This document provides instructions for a lab on SQL queries using subqueries. It introduces subqueries and provides examples of single and multiple row subqueries. It lists objectives and requirements for the lab. It then provides tasks for students to write SQL queries using subqueries to answer questions about movies and customers.
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)
22 views4 pages

Lab 08

This document provides instructions for a lab on SQL queries using subqueries. It introduces subqueries and provides examples of single and multiple row subqueries. It lists objectives and requirements for the lab. It then provides tasks for students to write SQL queries using subqueries to answer questions about movies and customers.
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/ 4

Department of Mathematics

CS220: Database Systems

Class: BS Mathematics Semester 4


Lab 08: SQL Queries (Subqueries) and Data Modelling

Date: April 14th, 2021


Time: 1:00-03:00
Instructor: Ms. Naheeda Parveen

Lab Engineer: Sundas Dawood

CS220: Database Systems Page 1


Lab 08: SQL Queries (Subqueries)
Introduction
The sub query in SQL is a query within query. A sub- query is composed of an outer query and
an inner query.

Objectives
After completing this lab, you should be able to do the following:

 Understand and practice the single row sub queries


 Understand and practice the multiple row sub queries

Tools/Software Requirement
 MySQL Community Server
 MySQL Workbench

Description:

For example, you want to see all the employees whose salary is above average salary. For this
you have to first compute the average salary using AVG function and then compare employees’
salaries with this computed salary. This is possible using subquery. Here the sub query will first
compute the average salary and then main query will execute.

Select *
from emp
where sal >
(select avg(sal)
from emp);

Similarly we want to see the name and empno of that employee whose salary is maximum.
Select *
from emp
where sal = (select max(sal)
from emp);

We want to see how many employees are there whose salary is above average.

Select count(*)
CS220: Database Systems Page 2
from emp
where sal >
(select avg(sal)
from emp);

We want to see those employees who are working in 'DALLAS'. Remember emp and dept are
joined on deptno and loc column is in the dept table. Assuming that wherever the department is
located the employee is working in that city.
Select *
from emp

where deptno IN
(select deptno
from dept
where loc='DALLAS');

Lab Tasks

On the basis of the knowledge of the sub-queries, attempt the following questions using the
Subqueries concept. The information needs can also be met through join operations; however you
need to practice the subqueries. Try to implement subqueries concept as much as possible. You can
use join operation in your queries if required.

Write equivalent queries for at least two queries.

Write SQL queries for the following information needs using subquery approach.

a) Number of customers who never rented BREAKING HOME.

b) List accumulative replacement cost of the movies that were never rented.

c) Find customers who are associated to City Abu Dhabi or Aden.

d) What category does the movie COMA HEAD belong to?


e) Find the movies whose rental duration is less than the rental duration of movie ‘AFRICAN
EGG’.

f) Find those films whose category id is greater than every category id of available films.

Deliverables
CS220: Database Systems Page 3
Save all queries and their results in the word document that you are executing along with the
snapshots of the results. Upload this document to LMS.

CS220: Database Systems Page 4

You might also like