SlideShare a Scribd company logo
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

More Related Content

What's hot

sub Consultas Oracle SQL
sub Consultas Oracle SQLsub Consultas Oracle SQL
sub Consultas Oracle SQL
Alexander Calderón
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
Charan Reddy
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
Pyadav010186
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answers
Michael Belete
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with output
Nexus
 
DBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related QueriesDBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related Queries
Mohammad Imam Hossain
 
Sql views
Sql viewsSql views
Sql views
arshid045
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
Vikas Gupta
 
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
Al-Mamun Sarkar
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause
Deepam Aggarwal
 
Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQL
MSB Academy
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
TechandMate
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
pitchaiah yechuri
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
Mohammad Imam Hossain
 
Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testers
tlvd
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression tree
Shab Bi
 
Date and time functions in mysql
Date and time functions in mysqlDate and time functions in mysql
Date and time functions in mysql
V.V.Vanniaperumal College for Women
 
SQL practice questions - set 3
SQL practice questions - set 3SQL practice questions - set 3
SQL practice questions - set 3
Mohd Tousif
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
Mohan Kumar.R
 

What's hot (20)

sub Consultas Oracle SQL
sub Consultas Oracle SQLsub Consultas Oracle SQL
sub Consultas Oracle SQL
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answers
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with output
 
DBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related QueriesDBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related Queries
 
Sql views
Sql viewsSql views
Sql views
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
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
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause
 
Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQL
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testers
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression tree
 
Date and time functions in mysql
Date and time functions in mysqlDate and time functions in mysql
Date and time functions in mysql
 
SQL practice questions - set 3
SQL practice questions - set 3SQL practice questions - set 3
SQL practice questions - set 3
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
 

Viewers also liked

Sql wksht-9
Sql wksht-9Sql wksht-9
Sql wksht-9
Mukesh Tekwani
 
Sql wksht-7
Sql wksht-7Sql wksht-7
Sql wksht-7
Mukesh Tekwani
 
Sql ch 4
Sql ch 4Sql ch 4
Sql ch 4
Mukesh Tekwani
 
Sql wksht-5
Sql wksht-5Sql wksht-5
Sql wksht-5
Mukesh Tekwani
 
Sql wksht-2
Sql wksht-2Sql wksht-2
Sql wksht-2
Mukesh Tekwani
 
23246406 dbms-unit-1
23246406 dbms-unit-123246406 dbms-unit-1
23246406 dbms-unit-1
Piyush Kant Singh
 
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
DataWorks Summit
 
DBMS lab manual
DBMS lab manualDBMS lab manual
DBMS lab manual
maha tce
 
Sql queries
Sql queriesSql queries
Sql queries
narendrababuc
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
Mukesh Tekwani
 
Dbms viva questions
Dbms viva questionsDbms viva questions
Dbms viva questions
Balveer Rathore
 
DBMS Practical File
DBMS Practical FileDBMS Practical File
DBMS Practical File
Dushmanta Nath
 
Dbms lab questions
Dbms lab questionsDbms lab questions
Dbms lab questions
Parthipan Parthi
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
vijaybusu
 
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
iimjobs and hirist
 

Viewers also liked (15)

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

Similar to Sql wksht-1

Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
ANSHVAJPAI
 
SQL
SQLSQL
SQL sheet
SQL sheetSQL sheet
SQL sheet
Suman singh
 
SQL
SQLSQL
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2
Mohd Tousif
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
EllenGracePorras
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
Amin Choroomi
 
Dynamic websites lec2
Dynamic websites lec2Dynamic websites lec2
Dynamic websites lec2
Belal Arfa
 
Sql server lab_3
Sql server lab_3Sql server lab_3
Sql server lab_3
vijay venkatash
 
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)
Vidyasagar Mundroy
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
N.Jagadish Kumar
 
Sql
SqlSql
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
Huda Alameen
 
Sql
SqlSql
SQL Practice Question set
SQL Practice Question set SQL Practice Question set
SQL Practice Question set
Mohd Tousif
 
Query
QueryQuery
ADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptxADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptx
ArjayBalberan1
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
SabrinaShanta2
 
ADV Powepoint 4 Lec.pptx
ADV Powepoint 4 Lec.pptxADV Powepoint 4 Lec.pptx
ADV Powepoint 4 Lec.pptx
ArjayBalberan1
 
SQL.pptx
SQL.pptxSQL.pptx

Similar to 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)
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
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
 

More from Mukesh Tekwani

Computer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelComputer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube Channel
Mukesh 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).pdf
Mukesh Tekwani
 
Circular motion
Circular motionCircular motion
Circular motion
Mukesh Tekwani
 
Gravitation
GravitationGravitation
Gravitation
Mukesh 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 - Physics
Mukesh 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 conversion
Mukesh Tekwani
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion
Mukesh Tekwani
 
What is Gray Code?
What is Gray Code? What is Gray Code?
What is Gray Code?
Mukesh Tekwani
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversion
Mukesh 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-21
Mukesh 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 prism
Mukesh 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 surface
Mukesh Tekwani
 
Spherical mirrors
Spherical mirrorsSpherical mirrors
Spherical mirrors
Mukesh 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 atom
Mukesh 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 lenses
Mukesh 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 - WEIGHTAGE
Mukesh Tekwani
 
Cyber Laws
Cyber LawsCyber Laws
Cyber Laws
Mukesh Tekwani
 
XML
XMLXML

More from 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
 

Recently uploaded

Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 

Recently uploaded (20)

Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 

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