Writing Basic Writing Basic SQL statement SQL statement June 23, 2022 June 23, 2022 1 Create By Pantharee Sawasdimongkol Create By Pantharee Sawasdimongkol
Dec 22, 2015
Writing Basic Writing Basic SQL statementSQL statement
April 19, 2023April 19, 2023 11Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Objective Objective
After completing this lesson, you After completing this lesson, you should be able to do the following:should be able to do the following:
List the capabilities of SQL SELET List the capabilities of SQL SELET statementsstatements
Executer a basic SELECT statementExecuter a basic SELECT statement Differentiate between SQL Differentiate between SQL
statements and SQL*Plus commandsstatements and SQL*Plus commands
April 19, 2023April 19, 2023 22Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Capabilities of SQL SELECTCapabilities of SQL SELECTStatementsStatements
Selection Projection
Table 1
Table 1
Table 1
Table 2
Join
April 19, 2023April 19, 2023 33Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Basic SELECT Statement Basic SELECT Statement
SELECT identifies SELECT identifies whatwhat columns. columns. FROM identifies FROM identifies whichwhich table. table.
SELECT [ DISTINCT ] { * , column [ alias] , . . . }
FROM table;
April 19, 2023April 19, 2023 44Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Writing SQL Statement Writing SQL Statement
SQLSQL statements are not case sensitive.statements are not case sensitive. SQL statements can be on one or more SQL statements can be on one or more
lines.lines. Keywords cannot be abbreviated or Keywords cannot be abbreviated or
split across lines.split across lines. Clauses are usually placed on separate Clauses are usually placed on separate
lines.lines. Tabs and indents are used to enhance Tabs and indents are used to enhance
readability.readability.
April 19, 2023April 19, 2023 55Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Selecting All ColumnsSelecting All Columns
DEPTNODEPTNO DNAMEDNAME LOCLOC
---------------------------- ---------------------------- --------------------------
10 10 20 20 30 30
4040
ACCOUNTINGACCOUNTING
RESEARCHRESEARCH
SALESSALES
OPERATIONSOPERATIONS
NEW YORKNEW YORK
DALLASDALLAS
CHICAGOCHICAGO
BOSTONBOSTON
SQL> SELECT * 2 FROM dept;
April 19, 2023April 19, 2023 66Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Selecting Specific ColumnsSelecting Specific Columns
DEPTNO LOC ------------ ----------------- 10 NEW YORK 20 DALLAS 30 CHICAGO 40 BOSTON
SQL> SELECT deptno , loc 2 FROM dept;
April 19, 2023April 19, 2023 77Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Column Heading DefaultsColumn Heading Defaults
Default justification Default justification • Left: Date and character dataLeft: Date and character data• Right: Numeric dataRight: Numeric data
Default display: UppercaseDefault display: Uppercase
April 19, 2023April 19, 2023 88Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Arithmetic ExpressionsArithmetic Expressions
Create expressions on NUMBER and Create expressions on NUMBER and DATE data by using arithmetic DATE data by using arithmetic operators.operators.
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
April 19, 2023April 19, 2023 99Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Using Arithmetic OperatorsUsing Arithmetic Operators
ENAMEENAME SALSAL SAL+300SAL+300
-------------------------- ---------------------------- --------------------------
KINGKING
BLAKEBLAKE
CLARKCLARK
JONESJONES
MARTINMARTIN
ALLENALLEN
…… ……
50005000
28502850
24502450
29752975
12501250
16001600
53005300
31503150
27502750
32753275
15501550
19001900
SQL> SELECT ename , sal , sal+300 2 FROM emp;
14 rows selected
April 19, 2023April 19, 2023 1010Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Operator PrecedenceOperator Precedence
Multiplication and division take Multiplication and division take priority over addition and subtraction.priority over addition and subtraction.
Operators of the same priority are Operators of the same priority are evaluated from left to right.evaluated from left to right.
Parentheses are used to force Parentheses are used to force prioritized evaluation and to clarify prioritized evaluation and to clarify statements.statements.
* / + -
April 19, 2023April 19, 2023 1111Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Operator PrecedenceOperator Precedence
SQL> SELECT ename , sal , 100+12*sal 2 FROM emp;
ENAMEENAME SALSAL 12*SAL+10012*SAL+100
-------------------------- ---------------------------- --------------------------
KINGKING
BLAKEBLAKE
CLARKCLARK
JONESJONES
MARTINMARTIN
ALLENALLEN
…… ……
50005000
28502850
24502450
29752975
12501250
16001600
6010060100
3430034300
2950029500
3580035800
1510015100
1930019300
14 rows selected.
April 19, 2023April 19, 2023 1212Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Using ParenthesesUsing Parentheses
SQL> SELECT ename , sal , (100+12)*sal 2 FROM emp;
ENAMEENAME SALSAL 12*(SAL+100)12*(SAL+100)
-------------------------- ---------------------------- --------------------------
KINGKING
BLAKEBLAKE
CLARKCLARK
JONESJONES
MARTINMARTIN
…… ……
50005000
28502850
24502450
29752975
12501250
6120061200
3540035400
3060030600
3690036900
1620016200
14 rows selected.
April 19, 2023April 19, 2023 1313Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Defining a Null ValueDefining a Null Value A null is a value that is unavailable, unassigned, unknown, A null is a value that is unavailable, unassigned, unknown,
or inapplicable.or inapplicable. A null is not the same as zero or a blank space.A null is not the same as zero or a blank space.
SQL> SELECT ename , job , sal , comm 2 FROM emp;
ENAMEENAME JOBJOB SALSAL COMMCOMM
-------------------------- ---------------- ---------------- --------- --------- KINGKING
BLAKEBLAKE
…… ……
TURNERTURNER
…… ……
PRESIDENTPRESIDENT
MANAGERMANAGER
SALESMANSALESMAN
50005000
28502850
15001500 00
14 rows selected.
April 19, 2023April 19, 2023 1414Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Null Values Null Values in Arithmetic Expressionsin Arithmetic Expressions
Arithmetic expressions containing a Arithmetic expressions containing a null evaluate to null.null evaluate to null.
SQL> select ename , 12*sal + comm 2 from emp 3 where ename= ' KING';
ENAME 12*SAL+COMM------------ ---------------------KING
April 19, 2023April 19, 2023 1515Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Defining a Column AliasDefining a Column Alias
Renames a column headingRenames a column heading Is useful with calculationsIs useful with calculations Immediately follows column name; Immediately follows column name;
optional optional ASAS keyword between column keyword between column name and aliasname and alias
Requires double quotation marks if it Requires double quotation marks if it contains spaces or special characters contains spaces or special characters or is case sensitiveor is case sensitive
April 19, 2023April 19, 2023 1616Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Using Column AliasesUsing Column Aliases
SQL> SELECT ename AS name , sal salary 2 FROM emp;
NAME SALARY------------------- -----------------.. .. ..
SQL> SELECT ename “Name”, 2 sal * 12 “Annual Salary” 3 FROM emp;
Name Annual Salary------------------- -----------------.. .. ..
April 19, 2023April 19, 2023 1717Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Concatenation OperatorConcatenation Operator
Concatenates columns or character Concatenates columns or character strings to other columns strings to other columns
Is represented by two vertical bars Is represented by two vertical bars (||)(||)
Creates a resultant column that is a Creates a resultant column that is a character expressioncharacter expression
April 19, 2023April 19, 2023 1818Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Using the ConcatenationUsing the ConcatenationOperatorOperator
SQL> SELECT ename | | job As "Employees" 2 FROM emp;
Employees-----------------------------KINGPRESIDENTBLAKEMANAGERCLARKMANAGERJONESMANAGERMARTINSALESMANALLENSALESMAN……14 rows selected.
April 19, 2023April 19, 2023 1919Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Literal Character StringsLiteral Character Strings
A literal is a character, a number, or A literal is a character, a number, or a date included in the SELECT list.a date included in the SELECT list.
Date and characterDate and character literal values literal values must be enclosed within single must be enclosed within single quotation mark.quotation mark.
Each character string is output once Each character string is output once for each row returned.for each row returned.
April 19, 2023April 19, 2023 2020Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Using Literal Character StringsUsing Literal Character Strings
SQL> SELECT ename | | ' is a ' | | job 2 As “Employee Details" 3 FROM emp;
Employee Details-------------------------------------KING is a PRESIDENTBLAKE is a MANAGERCLARK is a MANAGERJONES is a MANAGERMARTIN is a SALESMAN……..14 rows selected.
April 19, 2023April 19, 2023 2121Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Duplicate RowsDuplicate Rows
The default display of queries is all The default display of queries is all rows including duplicate rows.rows including duplicate rows.
SQL> SELECT deptno2 FROM emp;
DEPTNO---------------------- 10 30 10 20
…….14 rows selected.
April 19, 2023April 19, 2023 2222Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Eliminating Duplicate RowsEliminating Duplicate Rows
Eliminate duplicate rows y using the Eliminate duplicate rows y using the DISTINCT keyword in the SELECT DISTINCT keyword in the SELECT clauseclause
SQL> SELECT DISTINCT deptno2 FROM emp;
DEPTNO ---------------------- 10 20 30
April 19, 2023April 19, 2023 2323Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
SQL and SQL*Plus Interaction SQL and SQL*Plus Interaction
Buffer
SQL *Plus
server
SQL Statements SQL Statements
SQL *Plus
Commands
Query Results
Formatted Report
April 19, 2023April 19, 2023 2424Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
SQL Statements VersusSQL Statements VersusSQL*Plus CommandsSQL*Plus Commands
SQLSQL A languageA language ANSI standardANSI standard Keyword cannot be Keyword cannot be
abbreviatedabbreviated Statements manipulate data Statements manipulate data
and table definitions in the and table definitions in the databasedatabase
SQL*PLUSSQL*PLUS An environment An environment Oracle proprietaryOracle proprietary Keywords can be Keywords can be
abbreviatedabbreviated Commands do not allow Commands do not allow
manipulation of values in manipulation of values in the databasethe database
SQL
statements
SQL
buffer
SQL * plus
commands
SQL * plus
buffer
April 19, 2023April 19, 2023 2525Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Overview of SQL*PlusOverview of SQL*Plus
Log in to SQL*Plus.Log in to SQL*Plus. Describe the table structure.Describe the table structure. Edit your SQL statement.Edit your SQL statement. Execute SQL from SQL*Plus.Execute SQL from SQL*Plus. Save SQL statements to files and Save SQL statements to files and
append SQL statements to files.append SQL statements to files. Execute saved files.Execute saved files. Load commands from file to buffer to Load commands from file to buffer to
edit.edit.April 19, 2023April 19, 2023 2626Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Logging In to SQL*PlusLogging In to SQL*Plus
From Windows environment:From Windows environment:
From command line:From command line:
SQL> [username[/password
[@database]]]April 19, 2023April 19, 2023 2727Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Displaying Table StructureDisplaying Table Structure
Use the SQL*Plus DESCRIBE command Use the SQL*Plus DESCRIBE command to display the structure of a table.to display the structure of a table.
DESC [ RIBE ] tablename
April 19, 2023April 19, 2023 2828Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Displaying Table StructureDisplaying Table Structure
DESC/DESCRIBE dept
NameName NULL?NULL? TypeType
-------------------------------- ---------------------------------------------- --------------------------
DEPTNODEPTNO
DNAMEDNAME
LOCLOC
NOT NULLNOT NULL NUMBER(2)NUMBER(2)
VARCHAR2(14)VARCHAR2(14)
VARCHAR2(13)VARCHAR2(13)
April 19, 2023April 19, 2023 2929Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
SQL*Plus Editing CommandsSQL*Plus Editing Commands
A[PPEND] A[PPEND] texttext C[HANGE] / old / newC[HANGE] / old / new C[HANGE] / text /C[HANGE] / text / CL[EAR] BUFF [ER]CL[EAR] BUFF [ER] DELDEL DEL DEL nn DEL DEL m nm n
April 19, 2023April 19, 2023 3030Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
SQL*Plus Editing CommandsSQL*Plus Editing Commands
I[NPUT]I[NPUT] I[NPUT] I[NPUT] texttext L[IST]L[IST] L[IST] L[IST] nn L[IST] L[IST] m nm n R[UN]R[UN] nn nn texttext 0 0 texttext
April 19, 2023April 19, 2023 3131Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
SQL*Plus File CommandsSQL*Plus File Commands
SAVE SAVE filenamefilename GET GET filenamefilename START START filenamefilename @ @ filenamefilename EDIT EDIT filenamefilename SPOOL SPOOL filenamefilename EXITEXIT
April 19, 2023April 19, 2023 3232Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
SummarySummary
Use SQL*Plus as an environment to:Use SQL*Plus as an environment to: Execute SQL statementsExecute SQL statements Edit SQL statementsEdit SQL statements
SQL> [ DISTINCT ] { * , column [ alias ] , …} 2 table;
April 19, 2023April 19, 2023 3333Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol
Practice OverviewPractice Overview
Selecting all data from different Selecting all data from different tablestables
Describing the structure of tablesDescribing the structure of tables Performing arithmetic calculations Performing arithmetic calculations
and specifying column namesand specifying column names Using SQL*Plus editorUsing SQL*Plus editor
April 19, 2023April 19, 2023 3434Create By Pantharee SawasdimongkolCreate By Pantharee Sawasdimongkol