The document contains 12 practice SQL exercises involving selecting attributes, sorting rows, selecting specific rows, and performing joins across multiple tables. The exercises cover basic queries to retrieve and filter data as well as more complex queries joining 3-4 tables. Sample SQL queries and solutions are provided for each exercise.
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
112 views
SQL Practice Problems
The document contains 12 practice SQL exercises involving selecting attributes, sorting rows, selecting specific rows, and performing joins across multiple tables. The exercises cover basic queries to retrieve and filter data as well as more complex queries joining 3-4 tables. Sample SQL queries and solutions are provided for each exercise.
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4
Practice Exercises SQL (1)
Selecting Attributes and Sorting
1.Produce a list showing Part_Number, Part_Description, On_Hand, and Price sorted by Part_Description. 2.We can reduce our shipping costs by sorting by Zip_Code. Produce a listing that does this and also shows the customer's name and address. 3.Produce a list showing Part_Number, Part_Description, On_Hand, and Price sorted by Warehouse and Class. In this case Warehouse is the major sort key and Class is the minor sort key. Selecting Rows 4.List all the attributes in ORDERLINE (might be called ORDER_PART) selecting only rows where the Number_Ordered is greater than or equal to 2. 5.List all customers Last_Name and First_Name Whose Credit_Limit is less than or equal to $1000. 6.List all customers Last_Name and First_Name Whose Credit_Limit is greater than or equal to $1000 and whose Zip_Code is 49219. 7.Using the PART table, select rows that have a Part_Number that begins with B Hint:WHERE SUBSTR (Part_Number, 1, 1) = B Note: the substring function uses Part_Number starting at position 1 for a length of 1. Thats how we can isolate the frst character of Part_Number. Joins 8.Using 2 tables, list the Part_Number, Part_Description, Number_Ordered and Quoted_Price. 9.Using 2 tables, list the Part_Number, Part_Description, Number_Ordered and Quoted_Price. The list should include only parts with a Part_Number that begins with C 10.Using 2 tables, list the Order_Number, Order_Date, Part_Number, Number_Ordered and Quoted_Price. 11. Using 3 tables, list the Order_Number, Order_Date, Part_Number, Part_Description, Number_Ordered and Quoted_Price 12.Using 4 tables, list the Customers Last_Name and First_Name followed by the Order_Number, Part_Description and Number_Ordered. ================================================= Answers
Practice Exercises SQL (1) Selecting Attributes and Sorting 1.Produce a list showing Part_Number, Part_Description, On_Hand, and Price sorted by Part_Description. SELECT Part_Number, Part_Description, On_Hand, Price FROM PREMIERE.PART ORDER BY Part_Description 2.We can reduce our shipping costs by sorting by Zip_Code. Produce a listing that does this and also shows the customer's name and address. SELECT Zip_Code, Last_Name, Street, City, State FROM PREMIERE.CUSTOMER ORDER BY Zip_Code 3.Produce a list showing Part_Number, Part_Description, On_Hand, and Price sorted by Warehouse and Class. In this case Warehouse is the major sort key and Class is the minor sort key. SELECT Part_Number, Part Description, On_Hand, Price FROM PREMIERE.PART ORDER BY Warehouse, Class Selecting Rows 4.List all the attributes in ORDERLINE (might be called ORDER_PART) selecting only rows where the Number_Ordered is greater than or equal to 2. SELECT * FROM PREMIERE.ORDERLINE WHERE Number_Ordered >= 2 5.List all customers Last_Name and First_Name Whose Credit_Limit is less than or equal to $1000. SELECT Last_Name, First_Name, Credit_Limit FROM PREMIERE.CUSTOMER WHERE Credit_Limit <= 1000 6.List all customers Last_Name and First_Name Whose Credit_Limit is greater than or equal to $1000 and whose Zip_Code is 49219. SELECT Last_Name, First_Name, Credit_Limit FROM PREMIERE.CUSTOMER WHERE Credit_Limit >= 1000 AND Zip_Code is 49219 7.Using the PART table, select rows that have a Part_Number that begins with B SELECT * FROM PREMIERE.PART WHERE SUBSTR (Part_Number, 1, 1) = B Note: the substring function uses Part_Number starting at position 1 for a length of 1. Thats how we can isolate the frst character of Part_Number. Joins 8.Using 2 tables, list the Part_Number, Part_Description, Number_Ordered and Quoted_Price. SELECT p.Part_Number, p.Part_Description, ol.Number_Ordered, ol.Quoted_Price FROM PREMIERE/ORDERLINE ol, PREMIERE/PART p WHERE p.Part_Number = ol.Part_Number Note: ORDERLINE (might be called ORDER_PART) 9.Using 2 tables, list the Part_Number, Part_Description, Number_Ordered and Quoted_Price. The list should include only parts with a Part_Number that begins with C SELECT p.Part_Number, p.Part_Description, ol.Number_Ordered, ol.Quoted_Price FROM PREMIERE/ORDERLINE ol, PREMIERE/PART p WHERE p.Part_Number = ol.Part_Number AND SUBSTR (Part_Number, 1, 1) = C 10. Using 2 tables, list the Order_Number, Order_Date, Part_Number, Number_Ordered and Quoted_Price. SELECT o.Order_Number, Order_Date, Part_Number, Number_Ordered, Quoted_Price FROM PREMIERE/ORDERS o, PREMIERE/ORDERLINE ol WHERE o.Order_Number = ol. Order_Number Note: ORDERLINE (might be called ORDER_PART) 11. Using 3 tables, list the Order_Number, Order_Date, Part_Number, Part_Description, Number_Ordered and Quoted_Price SELECT o.Order_Number, Order_Date, p.Part_Number, Part_Description, Number_Ordered, Quoted_Price FROM PREMIERE/ORDERS o, PREMIERE/ORDERLINE ol PREMIERE/PART p WHERE o.Order_Number = ol. Order_Number AND ol.Part_Number = p.Part_Number Note: ORDERLINE (might be called ORDER_PART) 12.Using 4 tables, list the Customers Last_Name and First_Name followed by the Order_Number, Part_Description and Number_Ordered. SELECT Last_Name, First_Name, o.Order_Number, Part_Description, Number_Ordered FROM PREMIERE/CUSTOMER c, PREMIERE.ORDERS o, PREMIERE/ORDERLINE ol, PREMIERE/PART p WHERE c.Customer_Number = o.Customer_Number AND o.Order_Number = ol. Order_Number AND ol.Part_Number = p.Part_Number Note: ORDERLINE (might be called ORDER_PART