les01.ppt
TRANSCRIPT
![Page 1: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/1.jpg)
1Writing Basic SQL SELECT Statements
![Page 2: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/2.jpg)
1-2
Objectives
After completing this lesson, you should be able to do the following:
• List the capabilities of SQL SELECT statements
• Execute a basic SELECT statement
• Differentiate between SQL statements and iSQL*Plus commands
![Page 3: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/3.jpg)
1-3
Capabilities of SQL SELECT Statements
SelectionProjection
Table 1 Table 2
Table 1Table 1
Join
![Page 4: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/4.jpg)
1-4
Basic SELECT Statement
SELECT *|{[DISTINCT] column|expression [alias],...}FROM table;
SELECT *|{[DISTINCT] column|expression [alias],...}FROM table;
• SELECT identifies what columns
• FROM identifies which table
![Page 5: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/5.jpg)
1-5
SELECT *FROM departments;
Selecting All Columns
![Page 6: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/6.jpg)
1-6
Selecting Specific Columns
SELECT department_id, location_idFROM departments;
![Page 7: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/7.jpg)
1-7
Writing SQL Statements
• SQL statements are not case sensitive.
• SQL statements can be on one or more lines.
• Keywords cannot be abbreviated or splitacross lines.
• Clauses are usually placed on separate lines.
• Indents are used to enhance readability.
![Page 8: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/8.jpg)
1-8
Arithmetic Expressions
Create expressions with number and date data by using arithmetic operators.
Operator
+
-
*
/
Description
Add
Subtract
Multiply
Divide
![Page 9: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/9.jpg)
1-9
Using Arithmetic Operators
SELECT last_name, salary, salary + 300FROM employees;
…
![Page 10: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/10.jpg)
1-10
Operator Precedence
• Multiplication and division take priority over addition and subtraction.
• Operators of the same priority are evaluated from left to right.
• Parentheses are used to force prioritized evaluation and to clarify statements.
**** //// ++++ ____
![Page 11: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/11.jpg)
1-11
Operator Precedence
SELECT last_name, salary, 12*salary+100FROM employees;
…
![Page 12: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/12.jpg)
1-12
Using Parentheses
SELECT last_name, salary, 12*(salary+100)FROM employees;
…
![Page 13: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/13.jpg)
1-13
Defining a Null Value
• A null is a value that is unavailable, unassigned, unknown, or inapplicable.
• A null is not the same as zero or a blank space.
SELECT last_name, job_id, salary, commission_pctFROM employees;
…
…
![Page 14: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/14.jpg)
1-14
SELECT last_name, 12*salary*commission_pctFROM employees;
Null Values in Arithmetic Expressions
Arithmetic expressions containing a null value evaluate to null.
…
…
![Page 15: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/15.jpg)
1-15
Defining a Column Alias
A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name - there can also be the optional AS keyword between the column name and alias
• Requires double quotation marks if it contains spaces or special characters or is case sensitive
![Page 16: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/16.jpg)
1-16
Using Column Aliases
SELECT last_name "Name", salary*12 "Annual Salary"FROM employees;
SELECT last_name AS name, commission_pct commFROM employees;
…
…
![Page 17: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/17.jpg)
1-17
Duplicate Rows
The default display of queries is all rows, including duplicate rows.
SELECT department_idFROM employees;
SELECT department_idFROM employees;
…
![Page 18: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/18.jpg)
1-18
Eliminating Duplicate Rows
Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.
SELECT DISTINCT department_idFROM employees;
![Page 19: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/19.jpg)
1-19
SQL Interaction
SQL statements
MySQLserver
Query resultsQuery results
Client
Formatted report
Internet Internet BrowserBrowser
SQL (PHP)SQL (PHP)
![Page 20: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/20.jpg)
1-20
Displaying Table Structure
Use the iSQL*Plus DESCRIBE command to display the structure of a table.
DESC[RIBE] tablenameDESC[RIBE] tablename
![Page 21: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/21.jpg)
1-21
Displaying Table Structure
DESCRIBE employeesDESCRIBE employees
![Page 22: Les01.ppt](https://reader034.vdocuments.net/reader034/viewer/2022051609/545e6ab5b1af9f0e598b47b4/html5/thumbnails/22.jpg)
1-22
Summary
SELECT *|{[DISTINCT] column|expression [alias],...}FROM table;
SELECT *|{[DISTINCT] column|expression [alias],...}FROM table;
In this lesson, you should have learned how to:
• Write a SELECT statement that:
– Returns all rows and columns from a table
– Returns specified columns from a table
– Uses column aliases to give descriptive column headings