Hour 7, Introduction to the Database Query

Team-Fly    

 
Sams Teach Yourself SQL in 24 Hours, Third Edition
By Ronald R. Plew, Ryan K. Stephens
Table of Contents
Appendix C.  Answers to Quizzes and Exercises


Hour 7, "Introduction to the Database Query"

Quiz Answers

1:

Name the required parts for any SELECT statement.

A1:

The SELECT and FROM keywords, also called clauses, are required for all SELECT statements.

2:

In the WHERE clause, are single quotation marks required for all the data?

A2:

No. Single quotation marks are required when selecting alphanumeric data types. Number data types do not require single quotation marks.

3:

Under what part of the SQL language does the SELECT statement (database query) fall?

A3:

The SELECT statement is considered Data Query Language.

4:

Can multiple conditions be used in the WHERE clause?

A4:

Yes. Multiple conditions can be specified in the WHERE clause of SELECT, INSERT, UPDATE, and DELETE statements. Multiple conditions are used with the operators AND and OR, which are thoroughly discussed next hour.

5:

What is the purpose of the DISTINCT option?

A5:

The DISTINCT option will suppress the display of duplicates.

Exercise Answers

1:

Invoke MySQL on your computer. Using your learnsql database, enter the following SELECT statements at the mysql> command prompt. Determine whether the syntax is correct. If the syntax is incorrect, make corrections to the code as necessary? We are using the EMPLOYEE_TBL here.

  1.  SELECT EMP_ID, LAST_NAME, FIRST_NAME,  FROM EMPLOYEE_TBL; 
  2.  SELECT EMP_ID, LAST_NAME  ORDER BY EMPLOYEE_TBL FROM EMPLOYEE_TBL; 
  3.  SELECT EMP_ID, LAST_NAME, FIRST_NAME  FROM EMPLOYEE_TBL WHERE EMP_ID = '213764555' ORDER BY EMP_ID; 
  4.  SELECT EMP_ID SSN, LAST_NAME  FROM EMPLOYEE_TBL WHERE EMP_ID = '213764555' ORDER BY 1; 
  5.  SELECT EMP_ID, LAST_NAME, FIRST_NAME  FROM EMPLOYEE_TBL WHERE EMP_ID = '213764555' ORDER BY 3, 1, 2; 
A1:
  1.  SELECT EMP_ID, LAST_NAME, FIRST_NAME,  FROM EMPLOYEE_TBL; 

    This SELECT statement does not work because there is a comma after the FIRST_NAME column that does not belong there. The correct syntax follows :

     SELECT EMPLOYEE_ID, LAST_NAME, FIRST_NAME,  FROM EMPLOYEE_TBL; 
  2.  SELECT EMP_ID, LAST_NAME  ORDER BY EMPLOYEE_TBL FROM EMPLOYEE_TBL; 

    This SELECT statement does not work because the FROM and ORDER BY clauses are in the incorrect order. The correct syntax follows:

     SELECT EMP_ID, LAST_NAME  FROM EMPLOYEE_TBL ORDER BY EM_ID; 
  3.  SELECT EMP_ID, LAST_NAME, FIRST_NAME  FROM EMPLOYEE_TBL WHERE EMP_ID = '213764555' ORDER BY EMP_ID; 

    The syntax for this SELECT statement is correct.

  4.  SELECT EMP_ID SSN, LAST_NAME  FROM EMPLOYEE_TBL WHERE EMP_ID = '213764555' ORDER BY 1; 

    The syntax for this SELECT statement is correct. Notice that the employee_id column is renamed SSN.

  5.  SELECT EMP_ID, LAST_NAME, FIRST_NAME  FROM EMPLOYEE_TBL WHERE EMP_ID = '213764555' ORDER BY 3, 1, 2; 

    Yes. The syntax is correct for this SELECT statement. Notice the order of the columns in the ORDER BY. This SELECT statement returns records from the database that are sorted by FIRST_NAME, and then by EMPLOYEE_ID, and finally by LAST_NAME.

2:

Does the following SELECT statement work?

 SELECT LAST_NAME, FIRST_NAME, PHONE  FROM EMPLOYEE_TBL WHERE EMP_ID = '333333333'; 
A2:

The syntax is correct and the statement worked even though no data was returned. No data was returned because there was no row with an EMP_ID of 333333333.

3:

Write a SELECT statement that returns the name and cost of each product from the PRODUCTS_TBL. Which product is the most expensive?

4:

Write a query that generates a list of all customers and their telephone numbers .


Team-Fly    
Top
 


Sams Teach Yourself SQL in 24 Hours
Sams Teach Yourself SQL in 24 Hours (5th Edition) (Sams Teach Yourself -- Hours)
ISBN: 0672335417
EAN: 2147483647
Year: 2002
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net