• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Sql wksht-1
 

Sql wksht-1

on

  • 882 views

SQL

SQL

Statistics

Views

Total Views
882
Views on SlideShare
882
Embed Views
0

Actions

Likes
1
Downloads
16
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Sql wksht-1 Sql wksht-1 Document Transcript

    • SQL Simple Queries – Ch 5 SQL Queries - Basics Worksheet - 11 USE Statement: This statement is used to open an existing database. Syntax USE { database } Arguments database Is the name of the database to which the user context is switched. Database names must conform to the rules for identifiers.2 Write SQL statement to open a database called pubs3 Write SQL statement to open a database called Northwind4 Write SQL statement to open an existing database called student5 SELECT clause Retrieves rows from the database and allows the selection of one or many rows or columns from one or many tables. SELECT select_list [ INTO new_table ] FROM table_source [ WHERE search_condition ] [ GROUP BY group_by_expression ] [ HAVING search_condition ] [ ORDER BY order_expression [ ASC | DESC ] ] E.g., Consider the database called pubs.6 To select all records from this table, we give the query: USE pubs SELECT * FROM authorsProf. Mukesh N. Tekwani Page 1 of 9
    • SQL Simple Queries – Ch 57 Syntax Diagram of SELECT statement:8 Write a query to select all records from a table called authors, and order the results in ascending order of lastname of author (au_lname), and ascending order of first name (au_fname). USE pubs SELECT * FROM authors ORDER BY au_lname ASC, au_fname ASC9 Write a query to return all rows, and only a subset of the columns (au_lname, au_fname, phone, city, state) from the authors table in the pubs database. Sort in ascending order of lastname and then by ascending order of first name. Column heading should be added to Telephone field. USE pubs SELECT au_fname, au_lname, phone AS Telephone, city, state FROM authors ORDER BY au_lname ASC, au_fname ASC10 Write a query to return all rows, and only a subset of the columns (au_lname, au_fname, phone, city, state) from the authors table in the pubs database. Sort in ascending order of lastname and then by ascending order of first name. Column headings should be added to lastname and first name fields.Page 2 of 9 mukeshtekwani@hotmail.com
    • SQL Simple Queries – Ch 511 Write a query to return all rows, and only a subset of the columns (au_lname, au_fname, phone, state) from the authors table in the pubs database, and only for authors staying in California. Phone column should have an appropriate heading. USE pubs SELECT au_fname, au_lname, phone AS Telephone, state FROM authors WHERE state = CA ORDER BY au_lname ASC, au_fname ASC12 Write a query to return all rows, and only a subset of the columns (au_lname, au_fname, phone, state) from the authors table in the pubs database, and only for authors not staying in California. Phone column should have an appropriate heading.13 List the sales offices with their targets and actual sales. SELECT CITY, TARGET, SALES FROM OFFICES14 List the Eastern region sales offices with their targets and sales. SELECT CITY, TARGET, SALES FROM OFFICES WHERE REGION = Eastern15 List Eastern region sales offices whose sales exceed their targets, sorted in alphabetical order by city. SELECT CITY, TARGET, SALES FROM OFFICES WHERE REGION = Eastern AND SALES > TARGET ORDER BY CITY16 What are the average target and sales for Eastern region offices? SELECT AVG(TARGET), AVG(SALES) FROM OFFICES WHERE REGION = EasternProf. Mukesh N. Tekwani Page 3 of 9
    • SQL Simple Queries – Ch 517 Show me a current list of our employees and their phone numbers." SELECT EmpLastName, EmpFirstName, EmpPhoneNumber FROM Employees18 "What are the names and prices of the products we carry, and under what category is each item listed?" SELECT ProductName, RetailPrice, Category FROM Products18 Show a list of subjects (SUBNAME), the category (CATID) each belongs to, and the code (SUBCODE) used in the catalog. Show the name first, followed by the category and then the code. SELECT SubjectName, CategoryID, SubjectCode FROM Subjects19 Consider a table called BOWLERS. Give a query to list of unique cities from this table. SELECT DISTINCT City FROM Bowlers20 Show a list of classes offered by the university, in alphabetical order. SELECT Category FROM Classes ORDER BY Category21 Select vendor name and ZIP Code from the vendors table and order by ZIP Code SELECT VendName, VendZipCode FROM Vendors ORDER BY VendZipCode22 Select vendor name and ZIP Code from the vendors table and order by ZIP Code in descending order. SELECT VendName, VendZipCode FROM Vendors ORDER BY VendZipCode DESC23 Select last name, first name, phone number, and employee ID from the employees table and order by last name and first name Select last name, first name, phone number, and employee ID from the employees tablePage 4 of 9 mukeshtekwani@hotmail.com
    • SQL Simple Queries – Ch 5 and order by last name and first name. SELECT EmpLastName, EmpFirstName, EmpPhoneNumber, EmployeeID FROM Employees ORDER BY EmpLastName, EmpFirstName24 Modify the above example to use the descending sort for last name and ascending sort for first name. SELECT EmpLastName, EmpFirstName, EmpPhoneNumber, EmployeeID FROM Employees ORDER BY EmpLastName DESC, EmpFirstName ASC25 Write SQL query to find out the five most expensive products in the Sales Orders database. SELECT TOP 5 ProductName, RetailPrice FROM Products ORDER BY RetailPrice DESC26 To find out the top 10% of products by price. SELECT TOP 10 PERCENT ProductName, RetailPrice FROM Products ORDER BY RetailPrice DESC27 Write a query to answer the following : “Which states do our customers come from?" SELECT DISTINCT CustState FROM Customers28 Give me a list of the buildings on campus and the number of floors for each building. Sort the list by building in ascending order." SELECT BuildingName, NumberOfFloors FROM Buildings ORDER BY BuildingName ASC29 Give me a list of all tournament dates and locations. I need the dates in descending order and the locations in alphabetical order." SELECT TourneyDate, TourneyLocation FROM Tournaments ORDER BY TourneyDate DESC, TourneyLocation ASCProf. Mukesh N. Tekwani Page 5 of 9
    • SQL Simple Queries – Ch 530 List the names, offices, and hire dates of all salespeople. SELECT NAME, REP_OFFICE, HIRE_DATE FROM SALESREPS31 What are the name, quota, and sales of employee number 107? SELECT NAME, QUOTA, SALES FROM SALESREPS WHERE EMPL_NUM = 10732 What are the average sales of our salespeople? SELECT AVG(SALES) FROM SALESREPS33 List the salespeople, their quotas, and their managers. SELECT NAME, QUOTA, MANAGER FROM SALESREPS34 List the city, region, and amount over/under target for each office. This involves a calculated value. SELECT CITY, REGION, (SALES - TARGET) FROM OFFICES35 Show the value of the inventory for each product. Inventory is qty * price SELECT MFR_ID, PRODUCT_ID, DESCRIPTION, (QTY_ON_HAND * PRICE) FROM PRODUCTS36 Show me the result if I raised each salespersons quota by 3 percent of their year-to- date sales. SELECT NAME, QUOTA, (QUOTA + (.03*SALES)) FROM SALESREPS37 List the name and month and year of hire for each salesperson. SELECT NAME, MONTH(HIRE_DATE), YEAR(HIRE_DATE) FROM SALESREPS38 SQL constants can be used by themselves as items in a select list. This can be useful for producing query results that are easier to read and interpret. SELECT CITY, has sales of, SALES FROM OFFICESPage 6 of 9 mukeshtekwani@hotmail.com
    • SQL Simple Queries – Ch 539 List the offices whose sales fall below 80 percent of target. SELECT CITY, SALES, TARGET FROM OFFICES WHERE SALES < (.8 * TARGET)40 Find salespeople hired before 1988. SELECT NAME FROM SALESREPS WHERE HIRE_DATE < 01-JAN-8841 Find orders placed in the last quarter of 1989. SELECT ORDER_NUM, ORDER_DATE, MFR, PRODUCT, AMOUNT FROM ORDERS WHERE ORDER_DATE BETWEEN 01-OCT-89 AND 31-DEC-8942 Find the orders that fall into various amount ranges. SELECT ORDER_NUM, AMOUNT FROM ORDERS WHERE AMOUNT BETWEEN 20000.00 AND 29999.9943 List the salespeople who work in New York, Atlanta, or Denver. SELECT NAME, QUOTA, SALES FROM SALESREPS WHERE REP_OFFICE IN (11, 13, 22)44 Find all orders placed with four specific salespeople. SELECT ORDER_NUM, REP, AMOUNT FROM ORDERS WHERE REP IN (107, 109, 101, 103) Note : The search condition X IN (A, B, C) is completely equivalent to:Prof. Mukesh N. Tekwani Page 7 of 9
    • SQL Simple Queries – Ch 5 (X = A) OR (X = B) OR (X = C)45 Wildcard Characters The percent sign (%) wildcard character matches any sequence of zero or more characters. This query uses the percent sign for pattern matching: SELECT COMPANY, CREDIT_LIMIT FROM CUSTOMERS WHERE COMPANY LIKE Smith% Corp.46 The underscore (_) wildcard character matches any single character. If you are sure that the companys name is either "Smithson" or "Smithsen," for example, you can use this query: SELECT COMPANY, CREDIT_LIMIT FROM CUSTOMERS WHERE COMPANY LIKE Smiths_n Corp. In this case, any of these names will match the pattern: Smithson Corp. Smithsen Corp. Smithsun Corp.47 Wildcard characters can appear anywhere in the pattern string, and several wildcard characters can be within a single string. This query allows either the "Smithson" or "Smithsen" spelling and will also accept "Corp.," "Inc.," or any other ending on the company name: SELECT COMPANY, CREDIT_LIMITPage 8 of 9 mukeshtekwani@hotmail.com
    • SQL Simple Queries – Ch 5 FROM CUSTOMERS WHERE COMPANY LIKE Smiths_n %48 Find products whose product IDs start with the four letters "A%BC". SELECT ORDER_NUM, PRODUCT FROM ORDERS WHERE PRODUCT LIKE A$%BC% ESCAPE $ Here we are using the $ character as the “Escape” character. The escape character is specified as a one-character constant string in the ESCAPE clause of the search condition. The first percent sign in the pattern, which follows an escape character, is treated as a literal percent sign; the second functions as a wildcard.Prof. Mukesh N. Tekwani Page 9 of 9