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

Practice 7 SQL Queries

This document contains 33 SQL queries applied to product and manufacturer tables in a database. The queries perform various operations like selecting, filtering, sorting, joining and aggregating data from the tables. Some example queries are: selecting the names and prices of all products, joining the product and manufacturer tables to show product names, prices and manufacturer names, filtering to find products from specific manufacturers that cost over a certain price. The queries demonstrate basic and advanced SQL features for querying and analyzing relational data.
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)
64 views

Practice 7 SQL Queries

This document contains 33 SQL queries applied to product and manufacturer tables in a database. The queries perform various operations like selecting, filtering, sorting, joining and aggregating data from the tables. Some example queries are: selecting the names and prices of all products, joining the product and manufacturer tables to show product names, prices and manufacturer names, filtering to find products from specific manufacturers that cost over a certain price. The queries demonstrate basic and advanced SQL features for querying and analyzing relational data.
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/ 29

PRIVATE UNIVERSITY OF TACNA

FACULTY OF ENGINEERING

Professional School of Systems Engineering

PRACTICE 7 SQL QUERIES

Course: Database Design

Teacher: Ing. Oliver Santana Carbajal

Student:

Puma Villa, Brayan (2015052787)

Tacna - Peru

2021-I
Queries on a table
1. List the name of all the products in the product table.

SELECT name

FROM product
2. Lists the names and prices of all products in the product table.

SELECT name, price

FROM product

3. List all the columns of the product table.

SELECT *

FROM product

4. List the name of the products, the price in Euros and the price in US dollars (USD).

SELECT name, price, price * 1.15

FROM product

5. List the name of the products, the price in Euros and the price in U.S. dollars (USD). Use the
following aliases for the columns: product name, euros, dollars.

SELECT name AS 'product name', price AS euro, price * 1.15 AS dollars

FROM product
6. Lists the names and prices of all products in the product table, converting the names to
uppercase.

SELECT UPPER(name), price

FROM product

7. Lists the names and prices of all products in the product table, converting the names to
lowercase.

SELECT LOWER(name), price

FROM product

8. List the name of all manufacturers in one column, and in another column capitalize the first
two characters of the manufacturer's name.

SELECT name, UPPER(SUBSTR(name, 1, 2))

FROM manufacturer

9. List the names and prices of all the products in the product table, rounding the price value.

SELECT name, ROUND(price)

FROM product
10. Lists the names and prices of all products in the product table, truncating the price value to
display it without any decimal places.

SELECT name, TRUNCATE(price)

FROM product

11. List the code of the manufacturers that have products in the product table.

SELECT manufacturer_code

FROM product

12. List the code of the manufacturers that have products in the product table, eliminating the
codes that are repeated.

SELECT DISTINCT manufacturer_code

FROM product

13. List the names of the manufacturers in ascending order.

SELECT name

FROM manufacturer

ORDER BY name ASC

14. List the names of the manufacturers in descending order.

SELECT name

FROM manufacturer

ORDER BY name DESC


15. List the product names sorted first by name in ascending order and second by price in
descending order.

SELECT name, price

FROM product

ORDER BY name ASC, price DESC

16. Returns a list with the first 5 rows of the manufacturer table.

SELECT *

FROM manufacturer

LIMIT 5

17. Returns a list with 2 rows starting from the fourth row of the manufacturer table. The fourth
row should also be included in the answer.

SELECT *

FROM manufacturer

LIMIT 3, 2

18. List the name and price of the cheapest product. (Use only ORDER BY and LIMIT clauses)

SELECT name, price

FROM product

ORDER BY price ASC

LIMIT 1
19. List the name and price of the most expensive product. (Use only ORDER BY and LIMIT
clauses)

SELECT name, price

FROM product

ORDER BY price DESC

LIMIT 1

20. List the name of all the manufacturer's products whose manufacturer code equals 2.

SELECT name

FROM product

WHERE manufacturer_code = 2

21. List the name of the products that have a price less than or equal to 120€.

SELECT name

FROM product

WHERE price <= 120

22. List the name of the products that have a price greater than or equal to 400€.

SELECT name

FROM product

WHERE price >= 400


23. List the name of the products that do not have a price greater than or equal to 400€.

SELECT name

FROM product

WHERE NOT price >= 400

24. List all products with a price between 80€ and 300€. Without using the BETWEEN operator.

SELECT *

FROM product

WHERE price >= 80 AND price <= 300

25. List all products with a price between 60€ and 200€. Using the BETWEEN operator.

SELECT *

FROM product

WHERE price BETWEEN 60 AND 200

26. List all products that have a price greater than 200€ and that the manufacturer code is equal
to 6.

SELECT *

FROM product

WHERE price > 200 AND manufacturer_code = 6

27. List all products where the manufacturer code is 1, 3 or 5. Without using the IN operator.
SELECT * FROM product

WHERE codigo_fabricante = 1 OR codigo_fabricante = 3 OR codigo_fabricante = 5

28. List all products where the manufacturer code is 1, 3 or 5. Using the IN operator.

SELECT * FROM product

WHERE manufacturer_code IN (1, 3, 5)

29. List the name and price of the products in cents (You will have to multiply the price value by
100). Create an alias for the column containing the price to be called cents.

SELECT name, price * 100 AS cents

FROM product

30. List the names of manufacturers whose name begins with the letter S.

SELECT name

FROM manufacturer

WHERE name LIKE 'S%'

31. List the names of manufacturers whose name ends with the vowel e.
SELECT nombre FROM fabricante

WHERE name LIKE '%e'

32. List the names of manufacturers whose name contains the character w.

SELECT nombre FROM fabricante

WHERE name LIKE '%w%'

33. List the names of manufacturers whose name is 4 characters long.

SELECT nombre FROM fabricante

WHERE name LIKE '____'

34. Returns a list with the name of all products containing the string Portable in the name.

SELECT nombre FROM producto

WHERE name LIKE '%Portable%'

35. Returns a list with the name of all products that contain the string Monitor in the name and
have a price less than 215 €.

SELECT nombre FROM producto

WHERE name LIKE '%Monitor%' AND price < 215

36. List the name and price of all products with a price greater than or equal to 180€. Sort the
result first by price (in descending order) and second by name (in ascending order).
SELECT name, price FROM product

WHERE price >= 180

ORDER BY price DESC, name ASC

Multi-Table Queries (Internal Composition)


Resolve all queries using SQL1 and SQL2 syntax.

Returns a list with the product name, price and manufacturer name of all products in the
database.

SELECT product.name, price, manufacturer.name

FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

2. Returns a list with the product name, price and manufacturer name of all products in the
database. Sort the result by manufacturer's name, in alphabetical order.

SELECT product.name, price, manufacturer.name

FROM product INNER JOIN manufacturer

ON product.manufacturer.code = manufacturer.code

ORDER BY manufacturer.name ASC

3. Returns a list with product code, product name, manufacturer code and manufacturer name,
of all products in the database.

SELECT product.code, product.name, manufacturer.code, manufacturer.name


FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

4. Returns the name of the product, its price and the name of its manufacturer, of the cheapest
product.

SELECT product.name, product.price, manufacturer.name

FROM product INNER JOIN manufacturer

ORDER BY product.price ASC

LIMIT 1

5. Returns the name of the product, its price and the name of its manufacturer, of the most
expensive product.

SELECT product.name, product.price, manufacturer.name

FROM product INNER JOIN manufacturer

ORDER BY product.price DESC

LIMIT 1

6. Returns a list of all Lenovo manufacturer products.

SELECT * FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code
WHERE manufacturer.name = 'Lenovo' WHERE manufacturer.name = 'Lenovo' WHERE
manufacturer.name = 'Lenovo

7. Return a list of all products from the manufacturer Crucial that have a price greater than 200€.

SELECT * FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

WHERE manufacturer.name = 'Crucial' AND product.price >200

8. Returns a list of all products from the manufacturers Asus, Hewlett-Packard and Seagate.
Without using the IN operator.

SELECT * FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

WHERE manufacturer.name = 'Asus' OR manufacturer.name = 'Hewlett-Packard' OR

manufacturer.name = 'Seagate

9. Returns a list of all products from the manufacturers Asus, Hewlett-Packard and Seagate.
Using the IN operator.

SELECT * FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

WHERE manufacturer.name IN ('Asus','Hewlett-Packard','Seagate')

10. Returns a list with the name and price of all the products of the manufacturer whose

name ending with the vowel e.


SELECT product.name, product.price FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

WHERE manufacturer.name LIKE ('%e')

11. Returns a list with the name and price of all products whose manufacturer name contains
the character w in its name.

SELECT product.name, product.price FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

WHERE manufacturer.name LIKE ('%w%')

12. Returns a list with product name, price and manufacturer name, of all the

products with a price greater than or equal to 180€. Order the result first by the

price (in descending order) and secondly by name (in ascending order)

SELECT product.name, product.price, manufacturer.name

FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

WHERE product.price >= 180

ORDER BY product.price DESC, product.name ASC

13. Returns a list with the code and manufacturer name of only those manufacturers that have
associated products in the database.

SELECT manufacturer.code, manufacturer.name FROM manufacturer INNER JOIN product


ON manufacturer.code = product.manufacturer_code

Multi-table queries (External composition)


Resolve all queries using the LEFT JOIN and RIGHT JOIN clauses.

1. Returns a list of all the manufacturers that exist in the database, together with the products
that each one of them has. The list should also show those manufacturers that do not have
associated products.

SELECT manufacturer.code, manufacturer.name, manufacturer.name, product.name

FROM manufacturer LEFT JOIN product

ON manufacturer.code = product.manufacturer_code

2. Returns a list with only those manufacturers that do not have any associated products.

SELECT manufacturer.name FROM manufacturer LEFT JOIN product

ON manufacturer.code != product.code_manufacturer

WHERE NOT manufacturer.code = ANY (

SELECT product.manufacturer_code

FROM product) LIMIT 10,2

3. Can there be products that are not related to a manufacturer? Justify your answer.

They cannot exist since the relationship in the relational model is 1 to many. This means that a
product can be created by a minimum of 1 manufacturer.
Summary Queries
Calculate the total number of products in the products table.

SELECT COUNT(*)

FROM product

2. Calculate the total number of manufacturers in the manufacturer table.

SELECT COUNT(*)

FROM manufacturer

3. Calculate the number of different manufacturer code values in the product table.

SELECT COUNT(DISTINCT manufacturer_code)

FROM product

4. Calculate the average price of all products.

SELECT AVG(price)

FROM product

5. Calculate the cheapest price of all products.

SELECT MIN(price)

FROM product
6. Calculate the most expensive price for all products.

SELECT MAX(price)

FROM product

7. List the name and price of the cheapest product.

SELECT name, price

FROM product

ORDER BY price ASC

LIMIT 1

8. List the name and price of the most expensive product.

SELECT name, price

FROM product

ORDER BY price DESC

LIMIT 1

9. Calculate the sum of the prices of all the products.

SELECT SUM(price)

FROM product

10. Calculate the number of products that the manufacturer Asus has.

SELECT COUNT(product.code)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Asus' WHERE manufacturer.name = 'Asus' WHERE


manufacturer.name = 'Asus
11. Calculate the average price of all products from the manufacturer Asus.

SELECT AVG(product.price)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Asus' WHERE manufacturer.name = 'Asus' WHERE


manufacturer.name = 'Asus

12. Calculate the cheapest price for all products from the manufacturer Asus.

SELECT MIN(product.price)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Asus' WHERE manufacturer.name = 'Asus' WHERE


manufacturer.name = 'Asus

13. Calculate the most expensive price for all products from the manufacturer Asus.

SELECT MAX(product.price)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Asus' WHERE manufacturer.name = 'Asus' WHERE


manufacturer.name = 'Asus

14. Calculate the sum of all products of the manufacturer Asus.

SELECT SUM(product.price)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Asus' WHERE manufacturer.name = 'Asus' WHERE


manufacturer.name = 'Asus

15. Displays the maximum price, minimum price, average price and total number of products
that the manufacturer Crucial has.

SELECT MAX(product.price), MIN(product.price), AVG(product.price), COUNT(product.code)

FROM manufacturer INNER JOIN product


ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Crucial' WHERE manufacturer.name = 'Crucial' WHERE


manufacturer.name = 'Crucial

16. Shows the total number of products that each manufacturer has. The listing should also
include manufacturers that do not have any products. The result will show two columns, one
with the name of the manufacturer and the other with the number of products it has. Sort the
result descending by the number of products.

SELECT manufacturer.name, COUNT(product.code)

FROM manufacturer LEFT JOIN product

ON product.manufacturer_code = manufacturer.code

GROUP BY manufacturer.code

ORDER BY 2 DESC

17. Shows the maximum price, minimum price and average price of each manufacturer's
products. The result will show the name of the manufacturer along with the requested data.

SELECT manufacturer.name, MAX(product.price), MIN(product.price), AVG(product.price)

FROM manufacturer INNER JOIN product

ON product.manufacturer_code = manufacturer.code

GROUP BY manufacturer.code

18. Shows the maximum price, minimum price, average price and the total number of products
of the manufacturers that have an average price higher than 200€. It is not necessary to show
the manufacturer's name, the manufacturer's code is sufficient.

SELECT manufacturer_code, MAX(price), MIN(price), AVG(price), COUNT(*)

FROM product

GROUP BY manufacturer_code

HAVING AVG(price) > 200


19. Displays the name of each manufacturer, along with the maximum price, minimum price,
average price and total number of products from manufacturers that have an average price
above 200€. It is necessary to show the name of the manufacturer.

SELECT manufacturer.name,

MAX(product.price), MIN(product.price),

AVG(product.price), COUNT(*)

FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

GROUP BY manufacturer.code

HAVING AVG(product.price) > 200

20. Calculate the number of products that have a price greater than or equal to 180€.

SELECT COUNT(*)

FROM product

WHERE price >= 180

21. Calculate the number of products that each manufacturer has with a price greater than or
equal to 180€.

SELECT manufacturer.name, COUNT(*)

FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

WHERE product.price >= 180

GROUP BY product.manufacturer_code

22. Lists the average price of each manufacturer's products, showing only the manufacturer's
code.

SELECT AVG(price), manufacturer_code

FROM product
GROUP BY manufacturer_code

23. Lists the average price of each manufacturer's products, showing only the manufacturer's
name.

SELECT AVG(price), manufacturer.name

FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

GROUP BY manufacturer.name

24. List the names of the manufacturers whose products have an average price greater than or
equal to 150€.

SELECT AVG(price), manufacturer.name

FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

GROUP BY manufacturer.name

HAVING AVG(price) >= 150

25. Returns a list with the names of manufacturers that have 2 or more products.

SELECT manufacturer.name, COUNT(product.code)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

GROUP BY manufacturer.code

HAVING COUNT(product.code) >= 2

26. Returns a list with the names of the manufacturers and the number of products that each
one has with a price greater than or equal to 220 €. It is not necessary to show the name of
manufacturers that do not have products that meet the condition. Example of the expected
result:
SELECT manufacturer.name, COUNT(product.code)
FROM manufacturer INNER JOIN product
ON manufacturer.code = product.manufacturer_code
WHERE product.price >= 220
GROUP BY manufacturer.code
ORDER BY 2 DESC

27. Returns a list with the names of the manufacturers and the number of products each one has
with a price greater than or equal to 220 €. The list must show the name of all manufacturers,
i.e., if there is a manufacturer that does not have products with a price greater than or equal to
220€, it must appear in the list with a value equal to 0 in the number of products.

(SELECT manufacturer.name, COUNT(product.code), COUNT(product.code))


FROM manufacturer INNER JOIN product
ON manufacturer.code = product.manufacturer_code
WHERE product.price >= 220
GROUP BY manufacturer.code)
UNION
(SELECT manufacturer.name, 0
FROM manufacturer
WHERE manufacturer.code NOT IN (
SELECT manufacturer.code
FROM manufacturer INNER JOIN product
ON manufacturer.code = product.manufacturer_code
WHERE product.price >= 220
GROUP BY manufacturer.code))
ORDER BY 2 DESC
28. Returns a list with the names of the manufacturers where the sum of the price of all their
products is greater than 1000 €.

SELECT manufacturer.name, SUM(product.price)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

GROUP BY manufacturer.code

HAVING SUM(product.price) > 1000

29. Returns a list with the name of the most expensive product that each manufacturer has. The
result should have three columns: product name, price and manufacturer's name. The result
must be sorted alphabetically from lowest to highest by manufacturer's name.

SELECT product.name, product.price, manufacturer.name

FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

WHERE product.price =

SELECT MAX(price)

FROM product

WHERE manufacturer_code = manufacturer.code

ORDER BY manufacturer.name ASC


Subqueries (In the WHERE clause)
With basic comparison operators

1. Return all Lenovo products. (Without using INNER JOIN).

SELECT * FROM product

WHERE manufacturer_code = (

SELECT code

FROM manufacturer

WHERE name = 'Lenovo')

2. Returns all data for products that have the same price as the most expensive product from
the manufacturer Lenovo. (Without using INNER JOIN).

SELECT * FROM product

WHERE price = (

SELECT MAX(price)

FROM product

WHERE product.manufacturer_code = (

SELECT code

FROM manufacturer

WHERE name = 'Lenovo'))

3. List the name of the most expensive product from the manufacturer Lenovo.

SELECT product.name

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Lenovo' AND product.price = (

SELECT MAX(price)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Lenovo')


4. List the name of the cheapest product from the manufacturer Hewlett-Packard.

SELECT product.name

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Hewlett-Packard' AND product.price = (

SELECT MIN(price)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Hewlett-Packard')

5. Returns all products in the database that have a price greater than or equal to the most
expensive product from the manufacturer Lenovo.

SELECT *

FROM product

WHERE price >= (

SELECT MAX(price)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Lenovo')

6. Lists all products of the manufacturer Asus that are priced above the average price of all its
products.

SELECT *

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Asus' AND product.price > (

SELECT AVG(price)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Asus')


Subqueries with ALL and ANY

8. Returns the most expensive product that exists in the table product without using MAX,
ORDER

BY ni LIMIT.

SELECT * FROM product

WHERE price >= ALL (

SELECT price

FROM product)

9. Returns the cheapest product that exists in the product table without using MIN, ORDER BY or
LIMIT.

SELECT * FROM product

WHERE price <= ALL (

SELECT price

FROM product)

10. Returns the names of the manufacturers that have associated products. (Using ALL or ANY).

SELECT nombre FROM fabricante

WHERE code = ANY (

SELECT manufacturer_code

FROM product)

11. Returns the names of manufacturers that have no associated products. (Using ALL or ANY).

SELECT nombre FROM fabricante


WHERE code != ALL (

SELECT manufacturer_code

FROM product)

Subqueries with IN and NOT IN

12. Returns the names of the manufacturers that have associated products. (Using IN or NOT
IN).

SELECT nombre FROM fabricante

WHERE code IN (SELECT manufacturer_code FROM product)

13. Returns the names of manufacturers that have no associated products. (Using IN or NOT IN).

SELECT nombre FROM fabricante

WHERE code NOT IN (SELECT code_manufacturer FROM product)

Subqueries with EXISTS and NOT EXISTS

14. Returns the names of the manufacturers that have associated products. (Using EXISTS or
NOT EXISTS).

SELECT nombre FROM fabricante

WHERE EXISTS (

SELECT manufacturer_code

FROM product

WHERE product.manufacturer.code = manufacturer.code)

15. Returns the names of manufacturers that have no associated products.

(Using EXISTS or NOT EXISTS).

SELECT nombre FROM fabricante

WHERE DOES NOT EXIST (


SELECT manufacturer_code

FROM product

WHERE product.manufacturer.code = manufacturer.code)

Correlated subqueries

16. List the name of each manufacturer with the name and price of its most expensive product.

SELECT product.name, product.price, manufacturer.name

FROM product INNER JOIN manufacturer

ON product.manufacturer_code = manufacturer.code

WHERE product.price =

SELECT MAX(price)

FROM product

WHERE manufacturer_code = manufacturer.code

17. Returns a list of all the products that have a price greater than or equal to the average of

all products from the same manufacturer.

SELECT *

FROM product AS p1

WHERE price >= (SELECT AVG(price)

FROM product AS p2

WHERE p1.manufacturer_code = p2.manufacturer_code)

18. List the name of the most expensive product from the manufacturer Lenovo.

SELECT product.name

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code
WHERE manufacturer.name = 'Lenovo' AND product.price = (

SELECT MAX(price)

FROM product

WHERE manufacturer_code = manufacturer.code)

Subqueries (In the HAVING clause)

19. Returns a list of all the manufacturer names that have the same number of products as the
manufacturer Lenovo.

SELECT manufacturer.name, COUNT(product.code)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

GROUP BY manufacturer.code

HAVING COUNT(product.code) >= (

SELECT COUNT(product.code)

FROM manufacturer INNER JOIN product

ON manufacturer.code = product.manufacturer_code

WHERE manufacturer.name = 'Lenovo')

You might also like