SlideShare une entreprise Scribd logo
1  sur  9
Télécharger pour lire hors ligne
SQL Simple Queries – Ch 5


                             SQL Queries - Basics
                                    Worksheet - 1
1     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 pubs


3     Write SQL statement to open a database called Northwind


4     Write SQL statement to open an existing database called student


5     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 authors




Prof. Mukesh N. Tekwani                                                            Page 1 of 9
SQL Simple Queries – Ch 5

7    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 ASC

9    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 ASC

10   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 5

11    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 ASC

12    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 OFFICES

14    List the Eastern region sales offices with their targets and sales.
      SELECT CITY, TARGET, SALES
      FROM OFFICES
      WHERE REGION = 'Eastern'

15    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 CITY

16    What are the average target and sales for Eastern region offices?
      SELECT AVG(TARGET), AVG(SALES)
      FROM OFFICES WHERE REGION = 'Eastern'

Prof. Mukesh N. Tekwani                                                        Page 3 of 9
SQL Simple Queries – Ch 5

17   Show me a current list of our employees and their phone numbers."
     SELECT EmpLastName, EmpFirstName, EmpPhoneNumber
     FROM Employees

18   "What are the names and prices of the products we carry, and under what category is
     each item listed?"
     SELECT ProductName, RetailPrice, Category
     FROM Products

18   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 Subjects

19   Consider a table called BOWLERS. Give a query to list of unique cities from this
     table.
     SELECT DISTINCT City
     FROM Bowlers

20   Show a list of classes offered by the university, in alphabetical order.
     SELECT Category
     FROM Classes
     ORDER BY Category


21   Select vendor name and ZIP Code from the vendors table and order by ZIP Code
     SELECT VendName, VendZipCode
     FROM Vendors
     ORDER BY VendZipCode

22   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 DESC

23   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 table

Page 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, EmpFirstName

24    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 ASC

25    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 DESC

26    To find out the top 10% of products by price.
      SELECT TOP 10 PERCENT ProductName, RetailPrice
      FROM Products
      ORDER BY RetailPrice DESC

27    Write a query to answer the following : “Which states do our customers come from?"
      SELECT DISTINCT CustState
      FROM Customers

28    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 ASC

29    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 ASC



Prof. Mukesh N. Tekwani                                                            Page 5 of 9
SQL Simple Queries – Ch 5

30   List the names, offices, and hire dates of all salespeople.
     SELECT     NAME,       REP_OFFICE,     HIRE_DATE
     FROM     SALESREPS

31   What are the name, quota, and sales of employee number 107?
     SELECT NAME, QUOTA, SALES
     FROM SALESREPS
     WHERE EMPL_NUM = 107

32   What are the average sales of our salespeople?
     SELECT AVG(SALES)
     FROM SALESREPS

33   List the salespeople, their quotas, and their managers.
     SELECT NAME, QUOTA, MANAGER
     FROM SALESREPS

34   List the city, region, and amount over/under target for each office. This involves a
     calculated value.
     SELECT CITY, REGION, (SALES - TARGET)
     FROM OFFICES

35   Show the value of the inventory for each product. Inventory is qty * price
     SELECT MFR_ID, PRODUCT_ID, DESCRIPTION, (QTY_ON_HAND * PRICE)
     FROM PRODUCTS

36   Show me the result if I raised each salesperson's quota by 3 percent of their year-to-
     date sales.
     SELECT NAME, QUOTA, (QUOTA + (.03*SALES))
     FROM SALESREPS

37   List the name and month and year of hire for each salesperson.
     SELECT NAME, MONTH(HIRE_DATE), YEAR(HIRE_DATE)
     FROM SALESREPS

38   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 OFFICES


Page 6 of 9                                                        mukeshtekwani@hotmail.com
SQL Simple Queries – Ch 5

39     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-88'


41    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-89'


42    Find the orders that fall into various amount ranges.
      SELECT ORDER_NUM, AMOUNT
      FROM ORDERS
      WHERE AMOUNT BETWEEN 20000.00 AND 29999.99


43    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 company's 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_LIMIT

Page 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

Contenu connexe

Tendances (20)

Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Aggregate function
Aggregate functionAggregate function
Aggregate function
 
8. sql
8. sql8. sql
8. sql
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
 
Join query
Join queryJoin query
Join query
 
Sql ch 13 - sql-views
Sql ch 13 - sql-viewsSql ch 13 - sql-views
Sql ch 13 - sql-views
 
MySQL JOIN & UNION
MySQL JOIN & UNIONMySQL JOIN & UNION
MySQL JOIN & UNION
 
Mysql datatypes
Mysql datatypesMysql datatypes
Mysql datatypes
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
Set operators
Set  operatorsSet  operators
Set operators
 
Sql Queries
Sql QueriesSql Queries
Sql Queries
 
SQL BASIC QUERIES
SQL  BASIC QUERIES SQL  BASIC QUERIES
SQL BASIC QUERIES
 
Sql joins
Sql joinsSql joins
Sql joins
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with output
 
AGGREGATE FUNCTION.pptx
AGGREGATE FUNCTION.pptxAGGREGATE FUNCTION.pptx
AGGREGATE FUNCTION.pptx
 
set operators.pptx
set operators.pptxset operators.pptx
set operators.pptx
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
 
Sql joins
Sql joinsSql joins
Sql joins
 
Sql task answers
Sql task answersSql task answers
Sql task answers
 

En vedette

Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testerstlvd
 
Analytical Queries with Hive: SQL Windowing and Table Functions
Analytical Queries with Hive: SQL Windowing and Table FunctionsAnalytical Queries with Hive: SQL Windowing and Table Functions
Analytical Queries with Hive: SQL Windowing and Table FunctionsDataWorks Summit
 
DBMS lab manual
DBMS lab manualDBMS lab manual
DBMS lab manualmaha tce
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing filesMukesh Tekwani
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questionsPyadav010186
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answersvijaybusu
 
Top 100 SQL Interview Questions and Answers
Top 100 SQL Interview Questions and AnswersTop 100 SQL Interview Questions and Answers
Top 100 SQL Interview Questions and Answersiimjobs and hirist
 

En vedette (18)

Sql wksht-3
Sql wksht-3Sql wksht-3
Sql wksht-3
 
Sql wksht-9
Sql wksht-9Sql wksht-9
Sql wksht-9
 
Sql wksht-7
Sql wksht-7Sql wksht-7
Sql wksht-7
 
Sql ch 4
Sql ch 4Sql ch 4
Sql ch 4
 
Sql wksht-5
Sql wksht-5Sql wksht-5
Sql wksht-5
 
Sql wksht-2
Sql wksht-2Sql wksht-2
Sql wksht-2
 
Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testers
 
23246406 dbms-unit-1
23246406 dbms-unit-123246406 dbms-unit-1
23246406 dbms-unit-1
 
Analytical Queries with Hive: SQL Windowing and Table Functions
Analytical Queries with Hive: SQL Windowing and Table FunctionsAnalytical Queries with Hive: SQL Windowing and Table Functions
Analytical Queries with Hive: SQL Windowing and Table Functions
 
DBMS lab manual
DBMS lab manualDBMS lab manual
DBMS lab manual
 
Sql queries
Sql queriesSql queries
Sql queries
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
 
Dbms viva questions
Dbms viva questionsDbms viva questions
Dbms viva questions
 
DBMS Practical File
DBMS Practical FileDBMS Practical File
DBMS Practical File
 
Dbms lab questions
Dbms lab questionsDbms lab questions
Dbms lab questions
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
 
Top 100 SQL Interview Questions and Answers
Top 100 SQL Interview Questions and AnswersTop 100 SQL Interview Questions and Answers
Top 100 SQL Interview Questions and Answers
 

Similaire à Sql wksht-1

Similaire à Sql wksht-1 (20)

Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
SQL
SQLSQL
SQL
 
SQL sheet
SQL sheetSQL sheet
SQL sheet
 
SQL
SQLSQL
SQL
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Dynamic websites lec2
Dynamic websites lec2Dynamic websites lec2
Dynamic websites lec2
 
Sql server lab_3
Sql server lab_3Sql server lab_3
Sql server lab_3
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Sql
SqlSql
Sql
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
 
Sql
SqlSql
Sql
 
SQL Practice Question set
SQL Practice Question set SQL Practice Question set
SQL Practice Question set
 
Query
QueryQuery
Query
 
ADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptxADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptx
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
 
ADV Powepoint 4 Lec.pptx
ADV Powepoint 4 Lec.pptxADV Powepoint 4 Lec.pptx
ADV Powepoint 4 Lec.pptx
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 

Plus de Mukesh Tekwani

Computer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelComputer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelMukesh Tekwani
 
The Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfThe Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfMukesh Tekwani
 
ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - PhysicsMukesh Tekwani
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion Mukesh Tekwani
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Mukesh Tekwani
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversionMukesh Tekwani
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion Mukesh Tekwani
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversionMukesh Tekwani
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Mukesh Tekwani
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prismMukesh Tekwani
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surfaceMukesh Tekwani
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomMukesh Tekwani
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesMukesh Tekwani
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEMukesh Tekwani
 

Plus de Mukesh Tekwani (20)

Computer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelComputer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube Channel
 
The Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfThe Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdf
 
Circular motion
Circular motionCircular motion
Circular motion
 
Gravitation
GravitationGravitation
Gravitation
 
ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - Physics
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversion
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion
 
What is Gray Code?
What is Gray Code? What is Gray Code?
What is Gray Code?
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversion
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prism
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surface
 
Spherical mirrors
Spherical mirrorsSpherical mirrors
Spherical mirrors
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atom
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lenses
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
 
Cyber Laws
Cyber LawsCyber Laws
Cyber Laws
 
XML
XMLXML
XML
 

Dernier

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 

Dernier (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

Sql wksht-1

  • 1. SQL Simple Queries – Ch 5 SQL Queries - Basics Worksheet - 1 1 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 pubs 3 Write SQL statement to open a database called Northwind 4 Write SQL statement to open an existing database called student 5 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 authors Prof. Mukesh N. Tekwani Page 1 of 9
  • 2. SQL Simple Queries – Ch 5 7 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 ASC 9 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 ASC 10 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
  • 3. SQL Simple Queries – Ch 5 11 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 ASC 12 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 OFFICES 14 List the Eastern region sales offices with their targets and sales. SELECT CITY, TARGET, SALES FROM OFFICES WHERE REGION = 'Eastern' 15 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 CITY 16 What are the average target and sales for Eastern region offices? SELECT AVG(TARGET), AVG(SALES) FROM OFFICES WHERE REGION = 'Eastern' Prof. Mukesh N. Tekwani Page 3 of 9
  • 4. SQL Simple Queries – Ch 5 17 Show me a current list of our employees and their phone numbers." SELECT EmpLastName, EmpFirstName, EmpPhoneNumber FROM Employees 18 "What are the names and prices of the products we carry, and under what category is each item listed?" SELECT ProductName, RetailPrice, Category FROM Products 18 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 Subjects 19 Consider a table called BOWLERS. Give a query to list of unique cities from this table. SELECT DISTINCT City FROM Bowlers 20 Show a list of classes offered by the university, in alphabetical order. SELECT Category FROM Classes ORDER BY Category 21 Select vendor name and ZIP Code from the vendors table and order by ZIP Code SELECT VendName, VendZipCode FROM Vendors ORDER BY VendZipCode 22 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 DESC 23 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 table Page 4 of 9 mukeshtekwani@hotmail.com
  • 5. SQL Simple Queries – Ch 5 and order by last name and first name. SELECT EmpLastName, EmpFirstName, EmpPhoneNumber, EmployeeID FROM Employees ORDER BY EmpLastName, EmpFirstName 24 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 ASC 25 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 DESC 26 To find out the top 10% of products by price. SELECT TOP 10 PERCENT ProductName, RetailPrice FROM Products ORDER BY RetailPrice DESC 27 Write a query to answer the following : “Which states do our customers come from?" SELECT DISTINCT CustState FROM Customers 28 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 ASC 29 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 ASC Prof. Mukesh N. Tekwani Page 5 of 9
  • 6. SQL Simple Queries – Ch 5 30 List the names, offices, and hire dates of all salespeople. SELECT NAME, REP_OFFICE, HIRE_DATE FROM SALESREPS 31 What are the name, quota, and sales of employee number 107? SELECT NAME, QUOTA, SALES FROM SALESREPS WHERE EMPL_NUM = 107 32 What are the average sales of our salespeople? SELECT AVG(SALES) FROM SALESREPS 33 List the salespeople, their quotas, and their managers. SELECT NAME, QUOTA, MANAGER FROM SALESREPS 34 List the city, region, and amount over/under target for each office. This involves a calculated value. SELECT CITY, REGION, (SALES - TARGET) FROM OFFICES 35 Show the value of the inventory for each product. Inventory is qty * price SELECT MFR_ID, PRODUCT_ID, DESCRIPTION, (QTY_ON_HAND * PRICE) FROM PRODUCTS 36 Show me the result if I raised each salesperson's quota by 3 percent of their year-to- date sales. SELECT NAME, QUOTA, (QUOTA + (.03*SALES)) FROM SALESREPS 37 List the name and month and year of hire for each salesperson. SELECT NAME, MONTH(HIRE_DATE), YEAR(HIRE_DATE) FROM SALESREPS 38 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 OFFICES Page 6 of 9 mukeshtekwani@hotmail.com
  • 7. SQL Simple Queries – Ch 5 39 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-88' 41 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-89' 42 Find the orders that fall into various amount ranges. SELECT ORDER_NUM, AMOUNT FROM ORDERS WHERE AMOUNT BETWEEN 20000.00 AND 29999.99 43 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
  • 8. 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 company's 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_LIMIT Page 8 of 9 mukeshtekwani@hotmail.com
  • 9. 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