6
IT Year II Semester II Evening– Group SL5 II. Premiere Products Exercise: 1. List the number and name of all customers. SELECT CustomerNum, CustomerName FROM Customer; 2. List the complete Part table. SELECT * FROM Part; 3. List the number and name of every customers represented by sales rep 35. SELECT CustomerNum, CustomerName FROM Customer WHERE RepNum='35'; 4. List the number and name of all customers that are represented by sales rep 35 and that have a credit limit of $10,000. SELECT CustomerNum, CustomerName FROM Customer WHERE RepNum='35' AND CreditLimit=10000; 5. List the number and name of all customers that are represented by sales rep 35 or that have a credit limit of $10,000. SELECT CustomerNum, CustomerName FROM Customer WHERE RepNum='35' OR CreditLimit=10000; 6. For each order, list the order number, order date, number of customer that placed the order, and name of the customer that placed the order. SELECT OrderNum, OrderDate, Orders.CustomerNum, CustomerName FROM Orders, Customer WHERE Orders.CustomerNum=Customer.CustomerNum; 7. List the number and name of all customers represented by Juan Perez. SELECT CustomerNum, CustomerName FROM Customer, Rep WHERE Customer.RepNum=Rep.RepNum AND LastName='Perez' AND FirstName='Juan'; 8. How many orders were place on 20/10/2013? SELECT COUNT(*) Access Page 1

SQL code

Embed Size (px)

DESCRIPTION

Setec institute

Citation preview

Page 1: SQL code

IT Year II Semester II Evening– Group SL5

II. Premiere Products Exercise: 1. List the number and name of all customers.

SELECT CustomerNum, CustomerNameFROM Customer;

2. List the complete Part table. SELECT *FROM Part;

3. List the number and name of every customers represented by sales rep 35. SELECT CustomerNum, CustomerNameFROM CustomerWHERE RepNum='35';

4. List the number and name of all customers that are represented by sales rep 35 and that have a credit limit of $10,000.

SELECT CustomerNum, CustomerNameFROM CustomerWHERE RepNum='35'AND CreditLimit=10000;

5. List the number and name of all customers that are represented by sales rep 35 or that have a credit limit of $10,000.

SELECT CustomerNum, CustomerNameFROM CustomerWHERE RepNum='35'OR CreditLimit=10000;

6. For each order, list the order number, order date, number of customer that placed the order, and name of the customer that placed the order.

SELECT OrderNum, OrderDate, Orders.CustomerNum, CustomerNameFROM Orders, CustomerWHERE Orders.CustomerNum=Customer.CustomerNum;

7. List the number and name of all customers represented by Juan Perez. SELECT CustomerNum, CustomerNameFROM Customer, RepWHERE Customer.RepNum=Rep.RepNumAND LastName='Perez'AND FirstName='Juan';

8. How many orders were place on 20/10/2013? SELECT COUNT(*)FROM OrdersWHERE OrderDate=#10/20/2010#;

9. Find the total of balances for all customers represented by sales rep 35. SELECT SUM(Balance)FROM CustomerWHERE RepNum='35';

10. Give the part number, description, and on-hand value (OnHand * Price) for each part in item class HW. SELECT PartNum, Description, OnHand * PriceFROM Part

Access Page 1

Page 2: SQL code

IT Year II Semester II Evening– Group SL5

WHERE Class='HW';

11. List all columns and all rows in the Part table. Sort the results by part description. SELECT *FROM PartORDER BY Description;

12. List all columns and all rows in the Part table. Sort the results by part number within item class. SELECT *FROM PartORDER BY Class, PartNum;

13. List the item class and the sum of the number of unit on hand. Group the result by item class. SELECT Class, SUM(OnHand)

FROM PartGROUP BY Class;

14. Create a new table named SportingGoods to contain the columns PartNum, Description, OnHand, Warhouse and Price for all rows in which the item class is SG SELECT PartNum, Description, OnHand, Warehouse, Price

INTO SportingGoodsFROM PartWHERE Class='SG';

15. In the SportingGoods table, change the description of part BV06 to “Fitness Gym.” UPDATE SportingGoodsSET Description='Fitness Gym'WHERE PartNum='BV06';

16. In the SportingGoods table, delete every row in which the price is greater than $1,000. DELETE FROM SportingGoodsWHERE Price>1000;

IV. Henry Books Case

1. List the name of each publisher that’s not located in New York.SELECT PublisherNameFROM PublisherWHERE NOT City='New York';

select Publisher.PublisherNameFrom Publisherwhere Publisher.City != "New York";

2. List the title of each book published by Penguin USA.SELECT TitleFROM BookWHERE PublisherCode='PE';

3. List the title of each book that has the type MYS.SELECT TitleFROM BookWHERE Type='MYS';

Access Page 2

Page 3: SQL code

IT Year II Semester II Evening– Group SL5

4. List the title of each book that has the type SFI and that is in paperback.SELECT TitleFROM BookWHERE Type='SFI'AND Paperback=Yes;

5. List the title of each book that has the type PSY or whose publisher code is JP.SELECT TitleFROM BookWHERE Type='PSY'OR PublisherCode='JP';

6. List the title of each book that has the type CMP, HIS, or SCI.SELECT TitleFROM BookWHERE Type IN ('CMP', 'HIS', 'SCI');

7. How many books have a publisher code of ST or VB?SELECT COUNT(*)FROM BOOKWHERE PublicsherCode IN(‘ST’,’VB’);

8. List the title of each book written by Dick Francis.SELECT TitleFROM Book, PublisherWHERE Book.PublisherCode=Publisher.PublisherCodeAND PublisherName=' Dick Francis ';

9. List the title of each book that has the type FIC and that was written by John Steinbeck.SELECT TitleFROM Book, PublisherWHERE Book.PublisherCode=Publisher.PublisherCodeAND Type=’FIC’AND PublisherName=’John Steinbeck’;

10. For each book with coauthors, list the title, publisher code, type and author names (in the order listed on the cover).SELECT Book.Title, Book.PublisherCode, Book.Type, group_concat(concat(Author.AuthorFirst," ",Author.AuthorLast)) as AuthorsFROM Book, Wrote, AuthorWHERE Book.BookCode = Wrote.BookCodeand Wrote.AuthorNum = Author.AuthorNumgroup by Wrote.BookCodehaving COUNT(Wrote.BookCode) = 2;

11. How many book copies have price that is greater than $20 but less than $25?SELECT count(*)FROM CopyWHERE Copy.Price > 20 and Copy.Price < 25;

Access Page 3

Page 4: SQL code

IT Year II Semester II Evening– Group SL5

12. List the branch number, and price for each copy of the Stranger.SELECT Copy.BranchNum, Copy.CopyNum, Copy.Quality, Copy.PriceFROM Copy, BookWHERE Copy.BookCode = Book.BookCode and Book.Title = "The Stranger";

13. List the branch name, and price for each copy of Electric Light.SELECT Branch.BranchName, Copy.CopyNum, Copy.Quality, Copy.PriceFROM Copy, Book, BranchWHERE Copy.BranchNum = Branch.BranchNumand Copy.BookCode = Book.BookCodeand Book.Title = "Electric Light";

14. For each book copy with a price greater than $25, list the book’s title, and price.SELECT Book.Title, Copy.Quality, Copy.PriceFROM Book, CopyWHERE Copy.BookCode = Book.BookCodeand Copy.Price > 25;

15. For each book copy available at the Henry on the Hill branch list the book’s title and author names (in the order listed on the cover). SELECT Book.Title, group_concat(concat(Author.AuthorFirst,' ',Author.AuthorLast)) as Authors

from Book, Author, Copy, Branch, WroteWHERE Book.BookCode = Copy.BookCode and Book.BookCode = Wrote.BookCodeand Wrote.AuthorNum = Author.AuthorNumand Copy.BranchNum = Branch.BranchNumand Branch.BranchName = "Henry on the Hill"group by Copy.BookCode;

16. Create a new table named FictionCopies using the data in the BookCode, Title, BranchNum, and Price columns for those books that have the type FIC.

SELECT BookCode, Title, BranchNum, PriceINTO FictionCopiesFROM Book

WHERE Type='FIC';17. Ray Henry is considering increasing the price of all copies of fiction books by 10%. To determine the new prices, list the book code, title, and increased price of every book in the FictionCopies table. (Your computed column should determine 110% of the current price, which is 100% plus a 10% increase.

SELECT BookCode, Title, Price+ Price*0.1 AS NewPrice FROM Book;

18. Use an update query to change the price of each book in the FictionCopies table with a current price of $14.00 to $14.50.

UPDATE FictionCopiesSET Price=14.50WHERE Price=14.00;

19. Use a delete query to delete all books in the FictionCopies table whose price is less then $7.00.DELETE FROM FictionCopiesWHERE Price<7;

Access Page 4