Practice 7 SQL Queries
Practice 7 SQL Queries
FACULTY OF ENGINEERING
Student:
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.
FROM product
SELECT *
FROM product
4. List the name of the products, the price in Euros and the price in US dollars (USD).
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.
FROM product
6. Lists the names and prices of all products in the product table, converting the names to
uppercase.
FROM product
7. Lists the names and prices of all products in the product table, converting the names to
lowercase.
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.
FROM manufacturer
9. List the names and prices of all the products in the product table, rounding the price value.
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.
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.
FROM product
SELECT name
FROM manufacturer
SELECT name
FROM manufacturer
FROM product
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)
FROM product
LIMIT 1
19. List the name and price of the most expensive product. (Use only ORDER BY and LIMIT
clauses)
FROM product
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
22. List the name of the products that have a price greater than or equal to 400€.
SELECT name
FROM product
SELECT name
FROM product
24. List all products with a price between 80€ and 300€. Without using the BETWEEN operator.
SELECT *
FROM product
25. List all products with a price between 60€ and 200€. Using the BETWEEN operator.
SELECT *
FROM product
26. List all products that have a price greater than 200€ and that the manufacturer code is equal
to 6.
SELECT *
FROM product
27. List all products where the manufacturer code is 1, 3 or 5. Without using the IN operator.
SELECT * FROM product
28. List all products where the manufacturer code is 1, 3 or 5. Using the IN operator.
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.
FROM product
30. List the names of manufacturers whose name begins with the letter S.
SELECT name
FROM manufacturer
31. List the names of manufacturers whose name ends with the vowel e.
SELECT nombre FROM fabricante
32. List the names of manufacturers whose name contains the character w.
34. Returns a list with the name of all products containing the string Portable in the name.
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 €.
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
Returns a list with the product name, price and manufacturer name of all products in the
database.
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.
ON product.manufacturer.code = manufacturer.code
3. Returns a list with product code, product name, manufacturer code and manufacturer name,
of all products in the database.
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.
LIMIT 1
5. Returns the name of the product, its price and the name of its manufacturer, of the most
expensive product.
LIMIT 1
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€.
ON product.manufacturer_code = manufacturer.code
8. Returns a list of all products from the manufacturers Asus, Hewlett-Packard and Seagate.
Without using the IN operator.
ON product.manufacturer_code = manufacturer.code
manufacturer.name = 'Seagate
9. Returns a list of all products from the manufacturers Asus, Hewlett-Packard and Seagate.
Using the IN operator.
ON product.manufacturer_code = manufacturer.code
10. Returns a list with the name and price of all the products of the manufacturer whose
ON product.manufacturer_code = manufacturer.code
11. Returns a list with the name and price of all products whose manufacturer name contains
the character w in its name.
ON product.manufacturer_code = manufacturer.code
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)
ON product.manufacturer_code = manufacturer.code
13. Returns a list with the code and manufacturer name of only those manufacturers that have
associated products in the database.
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.
ON manufacturer.code = product.manufacturer_code
2. Returns a list with only those manufacturers that do not have any associated products.
ON manufacturer.code != product.code_manufacturer
SELECT product.manufacturer_code
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
SELECT COUNT(*)
FROM manufacturer
3. Calculate the number of different manufacturer code values in the product table.
FROM product
SELECT AVG(price)
FROM product
SELECT MIN(price)
FROM product
6. Calculate the most expensive price for all products.
SELECT MAX(price)
FROM product
FROM product
LIMIT 1
FROM product
LIMIT 1
SELECT SUM(price)
FROM product
10. Calculate the number of products that the manufacturer Asus has.
SELECT COUNT(product.code)
ON manufacturer.code = product.manufacturer_code
SELECT AVG(product.price)
ON manufacturer.code = product.manufacturer_code
12. Calculate the cheapest price for all products from the manufacturer Asus.
SELECT MIN(product.price)
ON manufacturer.code = product.manufacturer_code
13. Calculate the most expensive price for all products from the manufacturer Asus.
SELECT MAX(product.price)
ON manufacturer.code = product.manufacturer_code
SELECT SUM(product.price)
ON manufacturer.code = product.manufacturer_code
15. Displays the maximum price, minimum price, average price and total number of products
that the manufacturer Crucial has.
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.
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.
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.
FROM product
GROUP BY manufacturer_code
SELECT manufacturer.name,
MAX(product.price), MIN(product.price),
AVG(product.price), COUNT(*)
ON product.manufacturer_code = manufacturer.code
GROUP BY manufacturer.code
20. Calculate the number of products that have a price greater than or equal to 180€.
SELECT COUNT(*)
FROM product
21. Calculate the number of products that each manufacturer has with a price greater than or
equal to 180€.
ON product.manufacturer_code = manufacturer.code
GROUP BY product.manufacturer_code
22. Lists the average price of each manufacturer's products, showing only the manufacturer's
code.
FROM product
GROUP BY manufacturer_code
23. Lists the average price of each manufacturer's products, showing only the manufacturer's
name.
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€.
ON product.manufacturer_code = manufacturer.code
GROUP BY manufacturer.name
25. Returns a list with the names of manufacturers that have 2 or more products.
ON manufacturer.code = product.manufacturer_code
GROUP BY manufacturer.code
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.
ON manufacturer.code = product.manufacturer_code
GROUP BY manufacturer.code
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.
ON product.manufacturer_code = manufacturer.code
WHERE product.price =
SELECT MAX(price)
FROM product
WHERE manufacturer_code = (
SELECT code
FROM manufacturer
2. Returns all data for products that have the same price as the most expensive product from
the manufacturer Lenovo. (Without using INNER JOIN).
WHERE price = (
SELECT MAX(price)
FROM product
WHERE product.manufacturer_code = (
SELECT code
FROM manufacturer
3. List the name of the most expensive product from the manufacturer Lenovo.
SELECT product.name
ON manufacturer.code = product.manufacturer_code
SELECT MAX(price)
ON manufacturer.code = product.manufacturer_code
SELECT product.name
ON manufacturer.code = product.manufacturer_code
SELECT MIN(price)
ON manufacturer.code = product.manufacturer_code
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
SELECT MAX(price)
ON manufacturer.code = product.manufacturer_code
6. Lists all products of the manufacturer Asus that are priced above the average price of all its
products.
SELECT *
ON manufacturer.code = product.manufacturer_code
SELECT AVG(price)
ON manufacturer.code = product.manufacturer_code
8. Returns the most expensive product that exists in the table product without using MAX,
ORDER
BY ni LIMIT.
SELECT price
FROM product)
9. Returns the cheapest product that exists in the product table without using MIN, ORDER BY or
LIMIT.
SELECT price
FROM product)
10. Returns the names of the manufacturers that have associated products. (Using ALL or ANY).
SELECT manufacturer_code
FROM product)
11. Returns the names of manufacturers that have no associated products. (Using ALL or ANY).
SELECT manufacturer_code
FROM product)
12. Returns the names of the manufacturers that have associated products. (Using IN or NOT
IN).
13. Returns the names of manufacturers that have no associated products. (Using IN or NOT IN).
14. Returns the names of the manufacturers that have associated products. (Using EXISTS or
NOT EXISTS).
WHERE EXISTS (
SELECT manufacturer_code
FROM product
FROM product
Correlated subqueries
16. List the name of each manufacturer with the name and price of its most expensive product.
ON product.manufacturer_code = manufacturer.code
WHERE product.price =
SELECT MAX(price)
FROM product
17. Returns a list of all the products that have a price greater than or equal to the average of
SELECT *
FROM product AS p1
FROM product AS p2
18. List the name of the most expensive product from the manufacturer Lenovo.
SELECT product.name
ON manufacturer.code = product.manufacturer_code
WHERE manufacturer.name = 'Lenovo' AND product.price = (
SELECT MAX(price)
FROM product
19. Returns a list of all the manufacturer names that have the same number of products as the
manufacturer Lenovo.
ON manufacturer.code = product.manufacturer_code
GROUP BY manufacturer.code
SELECT COUNT(product.code)
ON manufacturer.code = product.manufacturer_code