SQL 1
SQL 1
Purpose: To create a merchant database with customer, supplier, customer_order, orderItem and
product table their respective relationships
In order to create customer table, we will write the following SQL statement
Step 7: To retrieve the supplier table, we can write the following SQL statement
In order to create the customer_order table, we will write the following SQL statement
Step 10: To retrieve the Customer_order table, we can write the following SQL statement
Select * from Customer_order
Quality Criteria: your output should look like this
Step 11: Here we will create OrderItem table (refer step 8)
Step 13: To retrieve the OrderItem table, we can write the following SQL statement
Select * from OrderItem
Quality Criteria: your output should look like this
Step 16: To retrieve the Product table, we can write the following SQL statement
Select * from Product
Step 3: here we want retrieve orders whose unit price is greater than 30, so we can write the
following SQL statement
Step 4: here we want retrieve to list products with order quantities greater than or equal to 15, so
we can write the following SQL statement
SELECT ProductName
FROM Product
WHERE Id IN (SELECT ProductId
FROM OrderItem
WHERE Quantity >= 15)
Quality Criteria: your output should look like this
Operation Sheet-1.3 where and order by clause
Operation title: where and order by clause
Step 1: Use the above merchant database from operation sheet 1.1
Step 2: here we want to retrieve 50% of the customers record, so we can write the following
SQL statement
Step 3: here we want to list all suppliers with the number of products they offer, so we can write
the following SQL statement
SELECT CompanyName,
ProductCount = (SELECT COUNT(P.id)
FROM [Product] P
WHERE P.SupplierId = S.Id)
FROM Supplier S order by companyname DESC
Quality Criteria: your output should look like this
Step 4: here we want to list all French customer cities (without duplicates)
Step 5: here we want to list all suppliers that have no fax, we can write the following query
Step 1: Use the above merchant database from operation sheet 1.1
Step 2: Here we want to list all products that are packaged in jars, we can write the following
query
SELECT *
FROM Product
WHERE Package LIKE '%jars%'
Quality Criteria: your output should look like this
Step 3: here we want to list customers with orders over $2000, we can write the following query
SELECT *
FROM Customers
WHERE EXISTS
(SELECT Id
FROM [customer_Order]
WHERE CustomerId = Customers.Id
AND TotalAmount > 2000)
Step 4: here we want to list customers who are from London or Paris, we can write the following
query
SELECT firstname
FROM Customers
WHERE City IN ('Paris','London')
Quality Criteria: your output should look like this
Operation Sheet-1.5 Working with Join
Operation title: Working with Join
Purpose: To show functionalities of Join, left join, right join and full join
Step 1: Use the above merchant database from operation sheet 1.1
Step 2: To List all suppliers with their products we can write the following query
Step 3: To list all suppliers and their products, including suppliers with no products we can write
the following query
Step 5: To match all customers and suppliers by country we can write the following query
SELECT C.FirstName, C.LastName, C.Country AS CustomerCountry,
S.Country AS SupplierCountry, S.CompanyName
FROM Customers C
FULL JOIN Supplier S ON C.Country = S.Country
ORDER BY C.Country, S.Country
Quality Criteria: your output should look like this
Operation Sheet-1.6 Working with union operator
Operation title: Working with union operator
Purpose: To show functionalities of Join, left join, right join and full join
Step 1: Use the above merchant database from operation sheet 1.1
Task 1
Step 1: To list all unique countries for customers and suppliers we can write the following
statement
SELECT Country
FROM Customers
UNION
SELECT Country
FROM Supplier
Quality Criteria: your output should look like this
Operation Sheet-1.7 Data Control Language
Operation title: Data Control Language
Step 1: Use the above merchant database from operation sheet 1.1
Step 2: Imagine we have two database administrators ababe and kebede and we want them to
create a table, insert and delete a data from the tables from the merchant database. So, we can
write the following query (suppose that kebede will grant permission for ababe)
Step 2: Calculate the total amount (UnitPrice * Quantity) for each order item and display the
result with the order item ID and the total amount.
Step 3: To retrieve the product names that start with the letter "A" and display them in uppercase.
Step 2: let us create a table called orders which stores orders of customers with their order id,
product name and the date, which the order is placed. We can write the table as shown below
Step 3: to insert order data in the orders table we can write the following statement
Step 4: From the previous table, if we want to retrieve the date in year, month, date format, we
can write the following query
Step 5: If we want to add 30 days to the "OrderDate", to find the payment date. We can write the
following query
Purpose: To sort aggregated data using group by, order by and having clause. And to be able to
back up the database.
Step 2: To retrieve the total order amount for each customer and sort the result in descending
order of the sum.
Step 3: To list the number of products for each supplier, sorted high to low.