18
Bordoloi and Bordoloi and Bock Bock Chapter 3 :Single Table Chapter 3 :Single Table Query Basics Query Basics

Bordoloi and Bock Chapter 3 :Single Table Query Basics

Embed Size (px)

Citation preview

Page 1: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

Chapter 3 :Single Table Query BasicsChapter 3 :Single Table Query Basics

Page 2: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

SIMPLE SELECT STATEMENTSSIMPLE SELECT STATEMENTS

• The main element in a SQL query is the SELECT The main element in a SQL query is the SELECT statement.statement.• A properly written SELECT statement will always A properly written SELECT statement will always produce a result in the form of one or more rows of produce a result in the form of one or more rows of output.output.• The SELECT statement chooses (selects) rows The SELECT statement chooses (selects) rows from one or more tables according to specific from one or more tables according to specific criteria. criteria.

Page 3: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

ExampleExample

SELECT *SELECT *FROM employee;FROM employee;EMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_MIDDLE_NAMEEMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_MIDDLE_NAME------------ ------------------------ ------------------------- --------------------------------------- ------------------------ ------------------------- ---------------------------999666666 Bordoloi Bijoy999666666 Bordoloi Bijoy999555555 Joyner Suzanne 999555555 Joyner Suzanne AA999444444 Zhu Waiman 999444444 Zhu Waiman ZZmore rows and columns will be displayedmore rows and columns will be displayed

• This query selects rows from the “employee” This query selects rows from the “employee” table.table.• The asterisk (*) tells Oracle to select (display) The asterisk (*) tells Oracle to select (display) all all columnscolumns contained in the table “employee”. contained in the table “employee”.

Page 4: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

ExampleExample

•The following SELECT statement produces an The following SELECT statement produces an identical output.identical output.

SELECT emp_ssn, emp_last_name, emp_first_name, emp_middle_name,SELECT emp_ssn, emp_last_name, emp_first_name, emp_middle_name, emp_address, emp_city, emp_state, emp_zip, emp_date_of_birth,emp_address, emp_city, emp_state, emp_zip, emp_date_of_birth, emp_salary, emp_parking_space, emp_gender,emp_salary, emp_parking_space, emp_gender, emp_dpt_number, emp_superssnemp_dpt_number, emp_superssnFROM employee;FROM employee;

Page 5: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

• Oracle will process the query regardless of Oracle will process the query regardless of whether you type the entire query on one line, or whether you type the entire query on one line, or indent.indent.

• There are no rules about how many words can There are no rules about how many words can be put on a line or where to break a line. be put on a line or where to break a line.

• Although Oracle does not require indenting, Although Oracle does not require indenting, indenting enhances readability. indenting enhances readability.

Indenting SQL CodeIndenting SQL Code

Page 6: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

• The following keywords are your signal to start The following keywords are your signal to start a new line.a new line.

» SELECTSELECT

» FROMFROM

» WHEREWHERE

» GROUP BYGROUP BY

» HAVINGHAVING

» ORDER BY ORDER BY

Indenting SQL CodeIndenting SQL Code

Page 7: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

• Specify the column names to be displayed in the Specify the column names to be displayed in the result set by typing the exact, complete column result set by typing the exact, complete column names. names.

• Separate each column name with a comma (,).Separate each column name with a comma (,).• Specify the name of the table after the FROM Specify the name of the table after the FROM

clause.clause.• Terminate the query with a semi-colon (;).Terminate the query with a semi-colon (;).

SELECT emp_ssn, emp_last_name, emp_first_nameSELECT emp_ssn, emp_last_name, emp_first_name

FROM employee;FROM employee;

Selecting Specific ColumnsSelecting Specific Columns

Page 8: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

• There are syntactical rules that must be followed There are syntactical rules that must be followed or Oracle gives an error message instead of the or Oracle gives an error message instead of the desired result table. desired result table.

• Oracle communicates errors in SELECT Oracle communicates errors in SELECT statements by providing unique error numbers statements by providing unique error numbers and accompanying error descriptions. and accompanying error descriptions.

Common ErrorsCommon Errors

Page 9: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

FROM Keyword MissingFROM Keyword Missing

• The next SELECT statement is missing the FROM The next SELECT statement is missing the FROM clause so that no table name has been specified.clause so that no table name has been specified.

• Without a table name, the database management Without a table name, the database management system does not know which table to query.system does not know which table to query.

SELECT emp_ssn;SELECT emp_ssn;

ERROR at line 1:ERROR at line 1:

ORA-00923: FROM keyword not found where expectedORA-00923: FROM keyword not found where expected

Page 10: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

THE DISTINCT CLAUSETHE DISTINCT CLAUSE• Oracle provides a means for eliminating duplicate rows Oracle provides a means for eliminating duplicate rows

in a result table through use of the DISTINCT keyword .in a result table through use of the DISTINCT keyword .SELECT emp_salarySELECT emp_salaryFROM employee;FROM employee;EMP_SALARY EMP_SALARY ----------------- ----------------- $55,000.00 $55,000.00 $43,000.00 $43,000.00 $43,000.00 $43,000.00 $25,000.00 $25,000.00 $25,000.00 $25,000.00 $30,000.00 $30,000.00 $38,000.00 $38,000.00 $25,000.00 $25,000.00 8 rows selected.8 rows selected.

Page 11: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

DISTINCTDISTINCT

• The query is rewritten using the DISTINCT The query is rewritten using the DISTINCT keyword to eliminate duplicate rows.keyword to eliminate duplicate rows.

SELECT DISTINCT emp_salarySELECT DISTINCT emp_salaryFROM employee;FROM employee; EMP_SALARYEMP_SALARY-------------------------------------- $25,000.00$25,000.00 $30,000.00$30,000.00 $38,000.00$38,000.00 $43,000.00$43,000.00 $55,000.00$55,000.00

Page 12: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

THE WHERE CLAUSETHE WHERE CLAUSE

• Specific rows can be selected by adding a WHERE Specific rows can be selected by adding a WHERE clause to the SELECT query. clause to the SELECT query.

SELECT emp_ssn, emp_last_name, emp_first_name, emp_salarySELECT emp_ssn, emp_last_name, emp_first_name, emp_salary

FROM employeeFROM employee

WHERE emp_salary >= 35000; WHERE emp_salary >= 35000;

EMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_SALARYEMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_SALARY

-------------- -------------------------- --------------------------- --------------------------------- -------------------------- --------------------------- -------------------

999666666 Bordoloi 999666666 Bordoloi Bijoy $55,000.00 Bijoy $55,000.00

999555555 Joyner Suzanne $43,000.00999555555 Joyner Suzanne $43,000.00

999444444 Zhu Waiman $43,000.00999444444 Zhu Waiman $43,000.00

more rows will be displayed…more rows will be displayed…

Page 13: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

Comparison OperatorsComparison Operators

Operator MeaningOperator Meaning == equal toequal to << less thanless than >> greater thangreater than >=>= greater than or equal togreater than or equal to <=<= less than or equal toless than or equal to !=!= not equal tonot equal to <><> not equal to not equal to !>!> not greater thannot greater than !<!< not less thannot less than

Page 14: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

Comparing Character DataComparing Character Data

• Comparison operators are not limited to numeric data.Comparison operators are not limited to numeric data.• They can also be used with columns containing They can also be used with columns containing

character data. character data. • If the value is a character string or date, you must If the value is a character string or date, you must

surround the value (string of characters) with which a surround the value (string of characters) with which a column is being compared with column is being compared with single quotation single quotation (' ')(' ') marks.marks.

SELECT emp_ssn, emp_last_name, emp_first_nameSELECT emp_ssn, emp_last_name, emp_first_name

FROM employeeFROM employee

WHERE emp_gender = 'M'; WHERE emp_gender = 'M';

Page 15: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

Comparing Character DataComparing Character Data

SELECT emp_ssn, emp_last_name, emp_first_nameSELECT emp_ssn, emp_last_name, emp_first_nameFROM employeeFROM employeeWHERE emp_gender = M; WHERE emp_gender = M;

ERROR at line 3: ERROR at line 3: ORA-00904: invalid column name ORA-00904: invalid column name

• Since the literal string value was not enclosed by single Since the literal string value was not enclosed by single quote marks, Oracle assumed the letter M to be a quote marks, Oracle assumed the letter M to be a column name.column name.

• There is no column named M in the table so an error There is no column named M in the table so an error was returned. was returned.

Page 16: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

THE ORDER BY CLAUSETHE ORDER BY CLAUSE

• Output from a SELECT statement can be sorted by Output from a SELECT statement can be sorted by using the optional ORDER BY clause. using the optional ORDER BY clause.

SELECT emp_last_name, emp_first_nameSELECT emp_last_name, emp_first_nameFROM employeeFROM employeeWHERE emp_last_name >= 'J'WHERE emp_last_name >= 'J'ORDER BY emp_last_name;ORDER BY emp_last_name;EMP_LAST_NAME EMP_FIRST_NAMEEMP_LAST_NAME EMP_FIRST_NAME------------------------- --------------------------------------------------- --------------------------Joshi Joshi Dinesh DineshJoyner Joyner Suzanne SuzanneMarkis Markis Marcia Marciamore rows will be displayedmore rows will be displayed……

Page 17: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

ORDER BY With ASC and DESCORDER BY With ASC and DESC

• To sort columns from high to low, or descending, an To sort columns from high to low, or descending, an optional keyword DESC must be specified.optional keyword DESC must be specified.

ASC - Ascending, low to high.ASC - Ascending, low to high. DESC - Descending, high to low. DESC - Descending, high to low.

• When ASC or DESC is used, it must be followed by the When ASC or DESC is used, it must be followed by the column name.column name.

Page 18: Bordoloi and Bock Chapter 3 :Single Table Query Basics

Bordoloi and BockBordoloi and Bock

ORDER BY With More Than One ColumnORDER BY With More Than One Column

• Sorting by multiple columns improves the look and Sorting by multiple columns improves the look and usability of the information.usability of the information.

SELECT emp_dpt_number, emp_last_name, emp_first_nameSELECT emp_dpt_number, emp_last_name, emp_first_nameFROM employeeFROM employeeORDER BY emp_dpt_number, emp_last_name;ORDER BY emp_dpt_number, emp_last_name;EMP_DPT_NUMBER EMP_LAST_NAME EMP_FIRST_NAMEEMP_DPT_NUMBER EMP_LAST_NAME EMP_FIRST_NAME---------------------------- -------------------------- ------------------------------------------- -------------------------- --------------- 1 1 Bordoloi Bordoloi Bijoy Bijoy

33 Amin Amin Hyder Hyder 3 3 Joyner Suzanne Joyner Suzanne 3 3 Markis Markis Marcia Marcia 7 7 Bock Bock Douglas Douglas 7 7 Joshi Joshi Dinesh Dinesh 7 7 Prescott Prescott Sherri Sherri 7 7 Zhu Zhu Waiman Waiman