Resolved SQL Server Exercises
Resolved SQL Server Exercises
Employees (Empleado) where only one output column is generated and this contains the
EmployeeID
Suppose we want to see those products (Table Products) whose values are
They range between 4 and 20 dollars.
List the fields of the products table that have exactly a price of 18, 19 and
10 dollars.
Find all the last names (LastName) in the Employees table that begin with
the letter <<S>>. Use the wildcard character %.
SELECT LastName FROM Employees WHERE LastName LIKE 'S%' – Here it is formed the
phrase with the wildcard
To retrieve the last name of Employees whose first letter starts between <<A>> and
Use the wildcard [ ].
Use the Northwind database. List all fields of the Suppliers table whose
column Region is NULL.
Using the PUBS database. Calculate the total sales of the year to date.
(ytd_sales) of all the books in the titles table.
Using the PUBS database. You can find out the average price of all the
books if the prices were to double ( table titles ).
Using the PUBS database. Show the highest sales value of the year.
of all the books in the titles table.
Using the PUBS database. Show the minimum sales value of the year.
(ytd_sales) of all the books in the titles table.
Using the PUBS database. Count the rows in the titles table.
Using the PUBS database. Count the data in the titles table, whose type (TYPE)
sea business.
Use Pubs SELECT COUNT(TYPE) FROM titles WHERE TYPE = 'business'
Using the PUBS database. List the sum of sales by year (ytd_sales)
to date, classifying them by type (TYPE) of title (titles).
Use Pubs SELECT TYPE, pub_id, SUM(ytd_sales) from titles Group by TYPE, pub_id
Using the last example. List only the groups whose pub_id is equal to
0877. Hint, use having
SELECT TYPE, pub_id, SUM(ytd_sales) FROM titles GROUP BY TYPE, pub_id HAVING
0877
From the PUBS database. Combine the stores and discounts tables to show that
store offers a discount and the type of discount
Use Pubs
stor_id
JOIN discounts d ON s.stor_id = d.stor_id
stor_id
s, discounts as d WHERE s.stor_id = d.stor_id
Use the same previous example but use the FULL instruction in the from.
OUTER JOIN.
store_id
stores s FULL OUTER JOIN discounts d ON s.stor_id = d.stor_id
Use the same previous example, but use the RIGHT statement in the FROM.
OUTER JOIN.
stor_id
stores s RIGHT OUTER JOIN discounts d ON s.stor_id = d.stor_id
Using the Northwind database. Show the Products (Product ID, Name)
and Unit Price) that have a unit price equal to the Maximum.
ProductID
FROM Products WHERE UnitPrice = (SELECT MAX(UnitPrice) FROM Products)
Using the Northwind database. Show the Products (Product ID, Name)
and Unit Price) that have a unit price equal to the Minimum.
ID
FROM Products WHERE UNITPRICE = (SELECT MIN(UNITPRICE) FROM
Products)
NOTE: When using the optional ALL operator, all rows are listed as results.
From both queries. If ALL is not used, the UNION is listed, meaning they are not listed.
duplicate data.
Make a union of the nested queries seen earlier. Using both.
union options (With and Without ALL).
ID
FROM Products WHERE UNITPRICE = (SELECT MIN(UNITPRICE) FROM
Products
UNION
ID
FROM Products WHERE UNITPRICE = (SELECT MIN(UNITPRICE) FROM
Products
ID
FROM Products WHERE UNITPRICE = (SELECT MIN(UNITPRICE) FROM
Products
UNION ALL
ProductID
FROM Products WHERE UNITPRICE = (SELECT MIN(UNITPRICE) FROM
Products