Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bordoloi and Bock Chapter 3 :Single Table Query Basics.

Similar presentations


Presentation on theme: "Bordoloi and Bock Chapter 3 :Single Table Query Basics."— Presentation transcript:

1 Bordoloi and Bock Chapter 3 :Single Table Query Basics

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

3 Bordoloi and Bock Example SELECT * FROM employee; EMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_MIDDLE_NAME ------------ ------------------------ ------------------------- --------------------------- 999666666 Bordoloi Bijoy 999555555 Joyner Suzanne A 999444444 Zhu Waiman Z more rows and columns will be displayed This query selects rows from the “employee” table. This query selects rows from the “employee” table. The asterisk (*) tells Oracle to select (display) all columns contained in the table “employee”. The asterisk (*) tells Oracle to select (display) all columns contained in the table “employee”.

4 Bordoloi and Bock Example The following SELECT statement produces an identical output.The following SELECT statement produces an identical output. 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_superssn emp_dpt_number, emp_superssn FROM employee;

5 Bordoloi and Bock Oracle will process the query regardless of whether you type the entire query on one line, or indent.Oracle will process the query regardless of whether you type the entire query on one line, or indent. There are no rules about how many words can be put on a line or where to break a line.There are no rules about how many words can be put on a line or where to break a line. Although Oracle does not require indenting, indenting enhances readability.Although Oracle does not require indenting, indenting enhances readability. Indenting SQL Code

6 Bordoloi and Bock The following keywords are your signal to start a new line.The following keywords are your signal to start a new line. »SELECT »FROM »WHERE »GROUP BY »HAVING »ORDER BY Indenting SQL Code

7 Bordoloi and Bock Specify the column names to be displayed in the result set by typing the exact, complete column names.Specify the column names to be displayed in the result set by typing the exact, complete column names. Separate each column name with a comma (,).Separate each column name with a comma (,). Specify the name of the table after the FROM clause.Specify the name of the table after the FROM clause. Terminate the query with a semi-colon (;).Terminate the query with a semi-colon (;). SELECT emp_ssn, emp_last_name, emp_first_name FROM employee; Selecting Specific Columns

8 Bordoloi and Bock There are syntactical rules that must be followed or Oracle gives an error message instead of the desired result table.There are syntactical rules that must be followed or Oracle gives an error message instead of the desired result table. Oracle communicates errors in SELECT statements by providing unique error numbers and accompanying error descriptions.Oracle communicates errors in SELECT statements by providing unique error numbers and accompanying error descriptions. Common Errors

9 Bordoloi and Bock FROM Keyword Missing The next SELECT statement is missing the FROM clause so that no table name has been specified.The next SELECT statement is missing the FROM clause so that no table name has been specified. Without a table name, the database management system does not know which table to query.Without a table name, the database management system does not know which table to query. SELECT emp_ssn; ERROR at line 1: ORA-00923: FROM keyword not found where expected

10 Bordoloi and Bock THE DISTINCT CLAUSE Oracle provides a means for eliminating duplicate rows in a result table through use of the DISTINCT keyword.Oracle provides a means for eliminating duplicate rows in a result table through use of the DISTINCT keyword. SELECT emp_salary FROM employee; EMP_SALARY----------------- $55,000.00 $55,000.00 $43,000.00 $43,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.

11 Bordoloi and BockDISTINCT The query is rewritten using the DISTINCT keyword to eliminate duplicate rows.The query is rewritten using the DISTINCT keyword to eliminate duplicate rows. SELECT DISTINCT emp_salary FROM employee; EMP_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

12 Bordoloi and Bock THE WHERE CLAUSE THE WHERE CLAUSE Specific rows can be selected by adding a WHERE clause to the SELECT query.Specific rows can be selected by adding a WHERE clause to the SELECT query. SELECT emp_ssn, emp_last_name, emp_first_name, emp_salary FROM employee WHERE emp_salary >= 35000; EMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_SALARY -------------- -------------------------- --------------------------- ------------------- 999666666 Bordoloi Bijoy $55,000.00 999555555 Joyner Suzanne $43,000.00 999444444 Zhu Waiman $43,000.00 more rows will be displayed…

13 Bordoloi and Bock Comparison Operators Operator Meaning =equal to =equal to <less than <less than >greater than >greater than >=greater than or equal to >=greater than or equal to <=less than or equal to <=less than or equal to !=not equal to !=not equal to <>not equal to <>not equal to !>not greater than !>not greater than !<not less than !<not less than

14 Bordoloi and Bock Comparing Character Data Comparing 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 character data.They can also be used with columns containing character data. If the value is a character string or date, you must surround the value (string of characters) with which a column is being compared with single quotation (' ') marks.If the value is a character string or date, you must surround the value (string of characters) with which a column is being compared with single quotation (' ') marks. SELECT emp_ssn, emp_last_name, emp_first_name FROM employee WHERE emp_gender = 'M';

15 Bordoloi and Bock Comparing Character Data Comparing Character Data SELECT emp_ssn, emp_last_name, emp_first_name FROM employee WHERE emp_gender = M; ERROR at line 3: ORA-00904: invalid column name Since the literal string value was not enclosed by single quote marks, Oracle assumed the letter M to be a column name.Since the literal string value was not enclosed by single quote marks, Oracle assumed the letter M to be a column name. There is no column named M in the table so an error was returned.There is no column named M in the table so an error was returned.

16 Bordoloi and Bock THE ORDER BY CLAUSE Output from a SELECT statement can be sorted by using the optional ORDER BY clause.Output from a SELECT statement can be sorted by using the optional ORDER BY clause. SELECT emp_last_name, emp_first_name FROM employee WHERE emp_last_name >= 'J' ORDER BY emp_last_name; EMP_LAST_NAME EMP_FIRST_NAME ------------------------- -------------------------- Joshi Dinesh Joyner Suzanne Markis Marcia more rows will be displayed …

17 Bordoloi and Bock ORDER BY With ASC and DESC To sort columns from high to low, or descending, an optional keyword DESC must be specified.To sort columns from high to low, or descending, an optional keyword DESC must be specified.  ASC - Ascending, low to high.  DESC - Descending, high to low. When ASC or DESC is used, it must be followed by the column name.When ASC or DESC is used, it must be followed by the column name.

18 Bordoloi and Bock ORDER BY With More Than One Column Sorting by multiple columns improves the look and usability of the information.Sorting by multiple columns improves the look and usability of the information. SELECT emp_dpt_number, emp_last_name, emp_first_name FROM employee ORDER BY emp_dpt_number, emp_last_name; EMP_DPT_NUMBER EMP_LAST_NAME EMP_FIRST_NAME ---------------------------- -------------------------- --------------- 1 Bordoloi Bijoy 1 Bordoloi Bijoy 3 Amin Hyder 3 Amin Hyder 3 Joyner Suzanne 3 Joyner Suzanne 3 Markis Marcia 3 Markis Marcia 7 Bock Douglas 7 Bock Douglas 7 Joshi Dinesh 7 Joshi Dinesh 7 Prescott Sherri 7 Prescott Sherri 7 Zhu Waiman 7 Zhu Waiman


Download ppt "Bordoloi and Bock Chapter 3 :Single Table Query Basics."

Similar presentations


Ads by Google