0% found this document useful (0 votes)
33 views8 pages

Lab 7 - (Queries II)

This document discusses SQL queries using GROUP BY and HAVING clauses. It provides examples of queries to: 1) Count the number of customers in each city and retrieve cities with more than one customer. 2) Find the average price of products and products that cost less than $15 with stock over 600. 3) Count the number of customers for each gender and in each city with more than one customer. The document demonstrates how to use GROUP BY to group query results and aggregate functions like COUNT, SUM, and AVG. It also explains how HAVING filters grouped results like WHERE but for aggregates. Sample queries and results are given to help learn SQL grouping, aggregation, and filtering

Uploaded by

jianmgamal
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)
33 views8 pages

Lab 7 - (Queries II)

This document discusses SQL queries using GROUP BY and HAVING clauses. It provides examples of queries to: 1) Count the number of customers in each city and retrieve cities with more than one customer. 2) Find the average price of products and products that cost less than $15 with stock over 600. 3) Count the number of customers for each gender and in each city with more than one customer. The document demonstrates how to use GROUP BY to group query results and aggregate functions like COUNT, SUM, and AVG. It also explains how HAVING filters grouped results like WHERE but for aggregates. Sample queries and results are given to help learn SQL grouping, aggregation, and filtering

Uploaded by

jianmgamal
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/ 8

23CSIS01P

Introduction to Information Systems

Lab (7)

QUERIES (USING SQL II


Lab (7)
In the previous lab, we learned how to manually write queries using SQL, we also mentioned some
basic SQL statements. In this lab, we will practice them more and learn some more SQL statements
such as “Having” and “Group By”.

We will use a new simple database schema for an online store. The relationships and data are
shown below:
Note: There are 4 main tables: 1:M relationship between Customer and Order & Product_type and
Product, as well as a M:N relationship between Orders and Product tables.

1
Customer table:

Orders table:

Product table:

Order_Product table:

2
Product_Type table:

1. Group By Clause:
 The GROUP BY clause summaries identical rows into a single/distinct group and
returns a single row with the summary for each group, by using appropriate
aggregate function in the SELECT list, like COUNT(), SUM(), MIN(), MAX(),
AVG(), etc.
 GROUP BY clause works on the rows returned by SELECT query.
SELECT <column_name>
FROM <table name>
[WHERE <filter condition>]
[GROUP BY <column/ column list>]

 Query 1: To retrieve the number of customers:


 Code:
SELECT COUNT(*) AS NumberOfCustomers
FROM Customer;
 Result:

3
 Query 2: To list the number of customers in each city:
 Code:

SELECT City, COUNT(*) AS NumberOfCustomers


FROM Customer
GROUP BY City;
OR
SELECT City, COUNT(Customer_ID) AS NumberOfCustomers
FROM Customer
GROUP BY City;  Result:

 Query 3: In each city, how many males and how many females?
 Code:
SELECT City, Gender, COUNT(*) AS NumberOfCustomers
FROM Customer
GROUP BY City, Gender
ORDER BY City, Gender;
Note: There will be an error if we removed either City or Gender from the
GROUP BY statement.  Result:

4
 Query 4: How many orders have been done in each year?
 Code:
SELECT YEAR(Order_date) AS orderYear, count(*)
AS NumberOfOrders
FROM Orders
GROUP BY YEAR(Order_date)
ORDER BY count(*);
 Result:

2. Having Clause:
 The HAVING clause works as a filter on top of grouped rows returned by
the GROUP BY clause (because the WHERE keyword could not be used
with aggregate functions).
 This clause cannot be replaced by WHERE clause and vice-versa.

SELECT <column_name>
FROM <table name>
[WHERE <filter condition>]
[GROUP BY <column/column list>]
[HAVING <filter on aggregate column>]

 Query 1: To retrieve how many products have the same price


(without using Having)  Code:
SELECT COUNT(Product_ID) AS NumberOfProducts,
Product_Price
FROM Product
GROUP BY Product_Price;
 Result:

5
 Query 2: To retrieve how many products have the same price (only if
they are more than 2 products)  Code:
SELECT COUNT(Product_ID) AS NumberOfProducts,
Product_Price
FROM Product
GROUP BY Product_Price
HAVING COUNT(Product_ID) > 2;
 Result:

Do It Yourself
1. Retrieve the cheap products (cost less than 15) with a large stock (greater than
600)
Expected result:

2. Retrieve the average price for the products.

Expected result:

3. Retrieve the number of customers for each gender.

Expected result:

4. Retrieve the number of customers in each city (only include cities that have
more than one customer)

6
Expected result:

You might also like