SlideShare a Scribd company logo
SQL- Introduction to MySQL
Introduction toIntroduction to
MySQLMySQL
ObjectivesObjectives
After completing this lesson, you should be
able to do the following:
◦ List the capabilities of SQL SELECT statements
◦ Execute a basic SELECT statement
BasicBasic SELECTSELECT StatementStatement
◦ SELECT identifies the columns to be displayed
◦ FROM identifies the table containing those columns
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table;
Selecting All ColumnsSelecting All Columns
SELECT *
FROM departments;
Selecting SpecificSelecting Specific
ColumnsColumns
SELECT department_id, location_id
FROM departments;
Writing SQL StatementsWriting SQL Statements
◦ SQL statements are not case-sensitive.
◦ SQL statements can be on one or more lines.
◦ Keywords cannot be abbreviated or split
across lines.
◦ Clauses are usually placed on separate lines.
◦ Indents are used to enhance readability.
◦ In SQL*plus, you are required to end each SQL statement
with a semicolon (;).
Arithmetic ExpressionsArithmetic Expressions
Create expressions with number and date data
by using arithmetic operators.
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
SELECT last_name, salary, salary + 300
FROM employees;
Using ArithmeticUsing Arithmetic
OperatorsOperators
…
SELECT last_name, salary, 12*salary+100
FROM employees;
Operator PrecedenceOperator Precedence
SELECT last_name, salary, 12*(salary+100)
FROM employees;
…
…
1
2
Defining a Column AliasDefining 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 if it is case-sensitive
Concatenation OperatorConcatenation Operator
A concatenation operator:
◦ Links columns or character strings to other columns
◦ Is represented by two vertical bars (||)
◦ Creates a resultant column that is a character expression
SELECT last_name||job_id AS "Employees"
FROM employees;
…
Literal Character StringsLiteral Character Strings
◦ A literal is a character, a number, or a date that is
included in the SELECT statement.
◦ Date and character literal values must be enclosed by
single quotation marks.
◦ Each character string is output once for each
row returned.
Using Literal CharacterUsing Literal Character
StringsStrings
…
SELECT last_name ||' is a '||job_id
AS "Employee Details"
FROM employees;
SummarySummary
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 display more descriptive column
headings
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table;
Practice 1:Practice 1:
1- The following SELECT statement executes successfully:
SELECT last_name, job_id, salary AS Sal
FROM employees;
True/False
2-The following SELECT statement executes successfully:
SELECT *
FROM job_grades;
True/False
3-There are four coding errors in the following statement.
Can you identify them?
SELECT employee_id, last_name
sal x 12 ANNUAL SALARY
FROM employees;
Practice 1 :Practice 1 :
4-The HR department needs a query to display all
unique job codes from the EMPLOYEES table.
5-The HR department has requested a report of all
employees and their job IDs. Display the last
name concatenated with the job ID (separated by
a comma and space) and name the column
Employee and Title.
Limiting Rows Using aLimiting Rows Using a
SelectionSelection
“retrieve all
employees in
department 90”
EMPLOYEES
…
Limiting the Rows ThatLimiting the Rows That
Are SelectedAre Selected
◦ Restrict the rows that are returned by using the WHERE
clause:
◦ The WHERE clause follows the FROM clause.
SELECT *|{[DISTINCT] column | expression [alias],...}
FROM table
[WHERE condition(s)];
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE department_id = 90 ;
Using theUsing the WHEREWHERE ClauseClause
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = 'Whalen' ;
Character Strings andCharacter Strings and
DatesDates
◦ Character strings and date values are enclosed by single
quotation marks.
◦ Character values are case-sensitive, and date values are
format-sensitive.
◦ The default date format is DD-MON-RR.
Comparison ConditionsComparison Conditions
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
BETWEEN
...AND...
Between two values (inclusive)
IN(set) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value
SELECT last_name, salary
FROM employees
WHERE salary <= 3000 ;
Using ComparisonUsing Comparison
ConditionsConditions
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500 ;
Using theUsing the BETWEENBETWEEN
ConditionCondition
Use the BETWEEN condition to display rows based
on a range of values:
Lower limit Upper limit
SELECT employee_id, last_name, salary, manager_id
FROM employees
WHERE manager_id IN (100, 101, 201) ;
Using theUsing the ININ ConditionCondition
Use the IN membership condition to test for
values in a list:
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%' ;
Using theUsing the LIKELIKE ConditionCondition
◦ Use the LIKE condition to perform wildcard searches of
valid search string values.
◦ Search conditions can contain either literal characters or
numbers:
 % denotes zero or many characters.
 _ denotes one character.
Using theUsing the LIKELIKE ConditionCondition
◦ You can combine pattern-matching characters:
SELECT last_name
FROM employees
WHERE last_name LIKE '_o%' ;
SELECT last_name, manager_id
FROM employees
WHERE manager_id IS NULL ;
Using theUsing the NULLNULL
ConditionsConditions
Test for nulls with the IS NULL operator.
Logical ConditionsLogical Conditions
Operator Meaning
AND Returns TRUE if both component
conditions are true
OR Returns TRUE if either component
condition is true
NOT Returns TRUE if the following
condition is false
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >=10000
AND job_id LIKE '%MAN%' ;
Using theUsing the ANDAND OperatorOperator
AND requires both conditions to be true:
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >= 10000
OR job_id LIKE '%MAN%' ;
Using theUsing the OROR OperatorOperator
OR requires either condition to be true:
SELECT last_name, job_id
FROM employees
WHERE job_id
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;
Using theUsing the NOTNOT OperatorOperator
Rules of PrecedenceRules of Precedence
Operator Meaning
1 Arithmetic operators
2 Concatenation operator
3 Comparison conditions
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 Not equal to
7 NOT logical condition
8 AND logical condition
9 OR logical condition
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)]
[ORDER BY {column, expr, alias} [ASC|DESC]] ;
SummarySummary
In this lesson, you should have learned how
to:
◦ Use the WHERE clause to restrict rows of output:
 Use the comparison conditions
 Use the BETWEEN, IN, LIKE, and NULL conditions
 Apply the logical AND, OR, and NOT operators
◦ Use the ORDER BY clause to sort rows of output
Practice 2:Practice 2:
1-The HR departments needs to find high-salary and low-salary
employees. display the last name and salary of employees who earn
between $5,000 and $12,000 and are in department 20 or 50. Label
the columns Employee and Monthly Salary, respectively.
2- Create a report to display the last name, salary, and commission of
all employees who earn commissions. Sort data in descending order
of salary and commissions.
3- Display the last name of all employees who have both an a and an e
in their last name.
4- Display the last name, job, and salary for all employees whose job
is SA_REP or ST_CLERKand whose salary is not equal to $2,500,
$3,500, or $7,000.
ThankThank You !!!You !!!
For More Information click below link:
Follow Us on:
http://vibranttechnologies.co.in/sql-classes-in-mumbai.htm

More Related Content

What's hot (20)

PPT
Les15
Vijay Kumar
 
PPTX
Where conditions and Operators in SQL
Raajendra M
 
PPT
Using the set operators
Syed Zaid Irshad
 
PPT
Retrieving data using the sql select statement
Syed Zaid Irshad
 
PPT
Les07 (using the set operators)
Achmad Solichin
 
PPT
Module03
Sridhar P
 
PPT
SQL select statement and functions
Vikas Gupta
 
PPT
Les18
Vijay Kumar
 
PPT
Les07
Sudharsan S
 
PPTX
MULTIPLE TABLES
ASHABOOPATHY
 
PPT
Sql select
Mudasir Syed
 
PPT
ALL ABOUT SQL AND RDBMS
gaurav koriya
 
PPT
SQL Introduction to displaying data from multiple tables
Vibrant Technologies & Computers
 
PPT
Sql server select queries ppt 18
Vibrant Technologies & Computers
 
PPT
Les17
Vijay Kumar
 
PPT
Les01 (retrieving data using the sql select statement)
Achmad Solichin
 
PPTX
1. dml select statement reterive data
Amrit Kaur
 
PPTX
Oracle: Basic SQL
DataminingTools Inc
 
PPT
Les01
Sudharsan S
 
PPT
Using subqueries to solve queries
Syed Zaid Irshad
 
Where conditions and Operators in SQL
Raajendra M
 
Using the set operators
Syed Zaid Irshad
 
Retrieving data using the sql select statement
Syed Zaid Irshad
 
Les07 (using the set operators)
Achmad Solichin
 
Module03
Sridhar P
 
SQL select statement and functions
Vikas Gupta
 
MULTIPLE TABLES
ASHABOOPATHY
 
Sql select
Mudasir Syed
 
ALL ABOUT SQL AND RDBMS
gaurav koriya
 
SQL Introduction to displaying data from multiple tables
Vibrant Technologies & Computers
 
Sql server select queries ppt 18
Vibrant Technologies & Computers
 
Les01 (retrieving data using the sql select statement)
Achmad Solichin
 
1. dml select statement reterive data
Amrit Kaur
 
Oracle: Basic SQL
DataminingTools Inc
 
Using subqueries to solve queries
Syed Zaid Irshad
 

Viewers also liked (20)

PPTX
E-mail System
Daniyal Khan
 
ODP
Object Oriented Design Patterns for PHP
RobertGonzalez
 
PDF
Introduction to PHP
Bradley Holt
 
PDF
PHP and Web Services
Bruno Pedro
 
PPTX
IS 151 Lecture 3
wajanga
 
PPT
IS 139 Lecture 7
wajanga
 
PPTX
IS 151 - Lecture 3
wajanga
 
PPT
Truth table analysis
Janet Stemwedel
 
PPTX
Chapter one
kiran acharya
 
PPTX
IS 151 Lecture 2
wajanga
 
PPT
Cd unit i
thulasib1
 
PPT
pointer, structure ,union and intro to file handling
Rai University
 
PPTX
aaالمملكة العربية السعودية
abdullah259
 
PPTX
Logical and Conditional Operator In C language
Abdul Rehman
 
PPTX
C Structures and Unions
Dhrumil Patel
 
PPT
Logic gates
prasanna chitra
 
PPTX
Hierarchical Memory System
Jenny Galino
 
PPT
C Structures & Unions
Ram Sagar Mourya
 
PPTX
Memory Hierarchy
Trinity Dwarka
 
E-mail System
Daniyal Khan
 
Object Oriented Design Patterns for PHP
RobertGonzalez
 
Introduction to PHP
Bradley Holt
 
PHP and Web Services
Bruno Pedro
 
IS 151 Lecture 3
wajanga
 
IS 139 Lecture 7
wajanga
 
IS 151 - Lecture 3
wajanga
 
Truth table analysis
Janet Stemwedel
 
Chapter one
kiran acharya
 
IS 151 Lecture 2
wajanga
 
Cd unit i
thulasib1
 
pointer, structure ,union and intro to file handling
Rai University
 
aaالمملكة العربية السعودية
abdullah259
 
Logical and Conditional Operator In C language
Abdul Rehman
 
C Structures and Unions
Dhrumil Patel
 
Logic gates
prasanna chitra
 
Hierarchical Memory System
Jenny Galino
 
C Structures & Unions
Ram Sagar Mourya
 
Memory Hierarchy
Trinity Dwarka
 
Ad

Similar to SQL- Introduction to MySQL (20)

PDF
0808.pdf
ssuser0562f1
 
PDF
Structure query language - Data Query language for beginners.pdf
munmunitjusl
 
PPT
Day1_Structured Query Language2_To understand.ppt
consravs
 
PPTX
Beginers guide for oracle sql
N.Jagadish Kumar
 
PPT
Day1_SQL_1 for learning about sql cldarly.ppt
consravs
 
PPTX
Practical 03 (1).pptx
solangiirfan92
 
PPT
Sql2
Nargis Ehsan
 
PDF
Basic Sql Handouts
jhe04
 
PPT
SQL WORKSHOP::Lecture 2
Umair Amjad
 
PPTX
Basic SQL Statments
Umair Shakir
 
PPT
01 basic orders
Soufiane Hakam
 
PPT
Sql oracle
Md.Abu Noman Shuvo
 
PPT
e computer notes - Restricting and sorting data
ecomputernotes
 
PPT
Les02 Restricting and Sorting Data using SQL.ppt
DrZeeshanBhatti
 
PDF
sql language
moman abde
 
PPTX
Data Manipulation Language.pptx
EllenGracePorras
 
PPT
Restricting and Sorting Data - Oracle Data Base
Salman Memon
 
PPT
SQL, consultas rapidas y sencillas, oracle
marycielocartagena73
 
PPTX
SQL(NEW).pptx
PoojaChawan2
 
PPT
Les02.ppt
gfhfghfghfgh1
 
0808.pdf
ssuser0562f1
 
Structure query language - Data Query language for beginners.pdf
munmunitjusl
 
Day1_Structured Query Language2_To understand.ppt
consravs
 
Beginers guide for oracle sql
N.Jagadish Kumar
 
Day1_SQL_1 for learning about sql cldarly.ppt
consravs
 
Practical 03 (1).pptx
solangiirfan92
 
Basic Sql Handouts
jhe04
 
SQL WORKSHOP::Lecture 2
Umair Amjad
 
Basic SQL Statments
Umair Shakir
 
01 basic orders
Soufiane Hakam
 
Sql oracle
Md.Abu Noman Shuvo
 
e computer notes - Restricting and sorting data
ecomputernotes
 
Les02 Restricting and Sorting Data using SQL.ppt
DrZeeshanBhatti
 
sql language
moman abde
 
Data Manipulation Language.pptx
EllenGracePorras
 
Restricting and Sorting Data - Oracle Data Base
Salman Memon
 
SQL, consultas rapidas y sencillas, oracle
marycielocartagena73
 
SQL(NEW).pptx
PoojaChawan2
 
Les02.ppt
gfhfghfghfgh1
 
Ad

More from Vibrant Technologies & Computers (20)

PPT
Buisness analyst business analysis overview ppt 5
Vibrant Technologies & Computers
 
PPT
SQL- Introduction to SQL database
Vibrant Technologies & Computers
 
PPT
ITIL - introduction to ITIL
Vibrant Technologies & Computers
 
PPT
Salesforce - Introduction to Security & Access
Vibrant Technologies & Computers
 
PPT
Data ware housing- Introduction to olap .
Vibrant Technologies & Computers
 
PPT
Data ware housing - Introduction to data ware housing process.
Vibrant Technologies & Computers
 
PPT
Data ware housing- Introduction to data ware housing
Vibrant Technologies & Computers
 
PPT
Salesforce - classification of cloud computing
Vibrant Technologies & Computers
 
PPT
Salesforce - cloud computing fundamental
Vibrant Technologies & Computers
 
PPT
SQL- Introduction to PL/SQL
Vibrant Technologies & Computers
 
PPT
SQL- Introduction to advanced sql concepts
Vibrant Technologies & Computers
 
PPT
SQL Inteoduction to SQL manipulating of data
Vibrant Technologies & Computers
 
PPT
SQL- Introduction to SQL Set Operations
Vibrant Technologies & Computers
 
PPT
Sas - Introduction to designing the data mart
Vibrant Technologies & Computers
 
PPT
Sas - Introduction to working under change management
Vibrant Technologies & Computers
 
PPT
SAS - overview of SAS
Vibrant Technologies & Computers
 
PPT
Teradata - Architecture of Teradata
Vibrant Technologies & Computers
 
PPT
Teradata - Restoring Data
Vibrant Technologies & Computers
 
PPT
Datastage database design and data modeling ppt 4
Vibrant Technologies & Computers
 
PPT
Datastage Introduction To Data Warehousing
Vibrant Technologies & Computers
 
Buisness analyst business analysis overview ppt 5
Vibrant Technologies & Computers
 
SQL- Introduction to SQL database
Vibrant Technologies & Computers
 
ITIL - introduction to ITIL
Vibrant Technologies & Computers
 
Salesforce - Introduction to Security & Access
Vibrant Technologies & Computers
 
Data ware housing- Introduction to olap .
Vibrant Technologies & Computers
 
Data ware housing - Introduction to data ware housing process.
Vibrant Technologies & Computers
 
Data ware housing- Introduction to data ware housing
Vibrant Technologies & Computers
 
Salesforce - classification of cloud computing
Vibrant Technologies & Computers
 
Salesforce - cloud computing fundamental
Vibrant Technologies & Computers
 
SQL- Introduction to PL/SQL
Vibrant Technologies & Computers
 
SQL- Introduction to advanced sql concepts
Vibrant Technologies & Computers
 
SQL Inteoduction to SQL manipulating of data
Vibrant Technologies & Computers
 
SQL- Introduction to SQL Set Operations
Vibrant Technologies & Computers
 
Sas - Introduction to designing the data mart
Vibrant Technologies & Computers
 
Sas - Introduction to working under change management
Vibrant Technologies & Computers
 
SAS - overview of SAS
Vibrant Technologies & Computers
 
Teradata - Architecture of Teradata
Vibrant Technologies & Computers
 
Teradata - Restoring Data
Vibrant Technologies & Computers
 
Datastage database design and data modeling ppt 4
Vibrant Technologies & Computers
 
Datastage Introduction To Data Warehousing
Vibrant Technologies & Computers
 

Recently uploaded (20)

PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Digital Circuits, important subject in CS
contactparinay1
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 

SQL- Introduction to MySQL

  • 3. ObjectivesObjectives After completing this lesson, you should be able to do the following: ◦ List the capabilities of SQL SELECT statements ◦ Execute a basic SELECT statement
  • 4. BasicBasic SELECTSELECT StatementStatement ◦ SELECT identifies the columns to be displayed ◦ FROM identifies the table containing those columns SELECT *|{[DISTINCT] column|expression [alias],...} FROM table;
  • 5. Selecting All ColumnsSelecting All Columns SELECT * FROM departments;
  • 6. Selecting SpecificSelecting Specific ColumnsColumns SELECT department_id, location_id FROM departments;
  • 7. Writing SQL StatementsWriting SQL Statements ◦ SQL statements are not case-sensitive. ◦ SQL statements can be on one or more lines. ◦ Keywords cannot be abbreviated or split across lines. ◦ Clauses are usually placed on separate lines. ◦ Indents are used to enhance readability. ◦ In SQL*plus, you are required to end each SQL statement with a semicolon (;).
  • 8. Arithmetic ExpressionsArithmetic Expressions Create expressions with number and date data by using arithmetic operators. Operator Description + Add - Subtract * Multiply / Divide
  • 9. SELECT last_name, salary, salary + 300 FROM employees; Using ArithmeticUsing Arithmetic OperatorsOperators …
  • 10. SELECT last_name, salary, 12*salary+100 FROM employees; Operator PrecedenceOperator Precedence SELECT last_name, salary, 12*(salary+100) FROM employees; … … 1 2
  • 11. Defining a Column AliasDefining 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 if it is case-sensitive
  • 12. Concatenation OperatorConcatenation Operator A concatenation operator: ◦ Links columns or character strings to other columns ◦ Is represented by two vertical bars (||) ◦ Creates a resultant column that is a character expression SELECT last_name||job_id AS "Employees" FROM employees; …
  • 13. Literal Character StringsLiteral Character Strings ◦ A literal is a character, a number, or a date that is included in the SELECT statement. ◦ Date and character literal values must be enclosed by single quotation marks. ◦ Each character string is output once for each row returned.
  • 14. Using Literal CharacterUsing Literal Character StringsStrings … SELECT last_name ||' is a '||job_id AS "Employee Details" FROM employees;
  • 15. SummarySummary 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 display more descriptive column headings SELECT *|{[DISTINCT] column|expression [alias],...} FROM table;
  • 16. Practice 1:Practice 1: 1- The following SELECT statement executes successfully: SELECT last_name, job_id, salary AS Sal FROM employees; True/False 2-The following SELECT statement executes successfully: SELECT * FROM job_grades; True/False 3-There are four coding errors in the following statement. Can you identify them? SELECT employee_id, last_name sal x 12 ANNUAL SALARY FROM employees;
  • 17. Practice 1 :Practice 1 : 4-The HR department needs a query to display all unique job codes from the EMPLOYEES table. 5-The HR department has requested a report of all employees and their job IDs. Display the last name concatenated with the job ID (separated by a comma and space) and name the column Employee and Title.
  • 18. Limiting Rows Using aLimiting Rows Using a SelectionSelection “retrieve all employees in department 90” EMPLOYEES …
  • 19. Limiting the Rows ThatLimiting the Rows That Are SelectedAre Selected ◦ Restrict the rows that are returned by using the WHERE clause: ◦ The WHERE clause follows the FROM clause. SELECT *|{[DISTINCT] column | expression [alias],...} FROM table [WHERE condition(s)];
  • 20. SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90 ; Using theUsing the WHEREWHERE ClauseClause
  • 21. SELECT last_name, job_id, department_id FROM employees WHERE last_name = 'Whalen' ; Character Strings andCharacter Strings and DatesDates ◦ Character strings and date values are enclosed by single quotation marks. ◦ Character values are case-sensitive, and date values are format-sensitive. ◦ The default date format is DD-MON-RR.
  • 22. Comparison ConditionsComparison Conditions Operator Meaning = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to BETWEEN ...AND... Between two values (inclusive) IN(set) Match any of a list of values LIKE Match a character pattern IS NULL Is a null value
  • 23. SELECT last_name, salary FROM employees WHERE salary <= 3000 ; Using ComparisonUsing Comparison ConditionsConditions
  • 24. SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500 ; Using theUsing the BETWEENBETWEEN ConditionCondition Use the BETWEEN condition to display rows based on a range of values: Lower limit Upper limit
  • 25. SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN (100, 101, 201) ; Using theUsing the ININ ConditionCondition Use the IN membership condition to test for values in a list:
  • 26. SELECT first_name FROM employees WHERE first_name LIKE 'S%' ; Using theUsing the LIKELIKE ConditionCondition ◦ Use the LIKE condition to perform wildcard searches of valid search string values. ◦ Search conditions can contain either literal characters or numbers:  % denotes zero or many characters.  _ denotes one character.
  • 27. Using theUsing the LIKELIKE ConditionCondition ◦ You can combine pattern-matching characters: SELECT last_name FROM employees WHERE last_name LIKE '_o%' ;
  • 28. SELECT last_name, manager_id FROM employees WHERE manager_id IS NULL ; Using theUsing the NULLNULL ConditionsConditions Test for nulls with the IS NULL operator.
  • 29. Logical ConditionsLogical Conditions Operator Meaning AND Returns TRUE if both component conditions are true OR Returns TRUE if either component condition is true NOT Returns TRUE if the following condition is false
  • 30. SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >=10000 AND job_id LIKE '%MAN%' ; Using theUsing the ANDAND OperatorOperator AND requires both conditions to be true:
  • 31. SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= 10000 OR job_id LIKE '%MAN%' ; Using theUsing the OROR OperatorOperator OR requires either condition to be true:
  • 32. SELECT last_name, job_id FROM employees WHERE job_id NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ; Using theUsing the NOTNOT OperatorOperator
  • 33. Rules of PrecedenceRules of Precedence Operator Meaning 1 Arithmetic operators 2 Concatenation operator 3 Comparison conditions 4 IS [NOT] NULL, LIKE, [NOT] IN 5 [NOT] BETWEEN 6 Not equal to 7 NOT logical condition 8 AND logical condition 9 OR logical condition
  • 34. SELECT *|{[DISTINCT] column|expression [alias],...} FROM table [WHERE condition(s)] [ORDER BY {column, expr, alias} [ASC|DESC]] ; SummarySummary In this lesson, you should have learned how to: ◦ Use the WHERE clause to restrict rows of output:  Use the comparison conditions  Use the BETWEEN, IN, LIKE, and NULL conditions  Apply the logical AND, OR, and NOT operators ◦ Use the ORDER BY clause to sort rows of output
  • 35. Practice 2:Practice 2: 1-The HR departments needs to find high-salary and low-salary employees. display the last name and salary of employees who earn between $5,000 and $12,000 and are in department 20 or 50. Label the columns Employee and Monthly Salary, respectively. 2- Create a report to display the last name, salary, and commission of all employees who earn commissions. Sort data in descending order of salary and commissions. 3- Display the last name of all employees who have both an a and an e in their last name. 4- Display the last name, job, and salary for all employees whose job is SA_REP or ST_CLERKand whose salary is not equal to $2,500, $3,500, or $7,000.
  • 36. ThankThank You !!!You !!! For More Information click below link: Follow Us on: http://vibranttechnologies.co.in/sql-classes-in-mumbai.htm

Editor's Notes

  • #4: Objectives To extract data from the database, you need to use the structured query language (SQL) SELECT statement. You may need to restrict the columns that are displayed. This lesson describes all the SQL statements that are needed to perform these actions. You may want to create SELECT statements that can be used more than once. This lesson also covers the iSQL*Plus environment in which you execute SQL statements.
  • #5: Basic SELECT Statement In its simplest form, a SELECT statement must include the following: A SELECT clause, which specifies the columns to be displayed A FROM clause, which identifies the table containing the columns that are listed in the SELECT clause In the syntax: SELECTis a list of one or more columns * selects all columns DISTINCTsuppresses duplicates column|expressionselects the named column or the expression aliasgives selected columns different headings FROM table specifies the table containing the columns Note: Throughout this course, the words keyword, clause, and statement are used as follows: A keyword refers to an individual SQL element.For example, SELECT and FROM are keywords. A clause is a part of a SQL statement.For example, SELECT employee_id, last_name, ... is a clause. A statement is a combination of two or more clauses.For example, SELECT * FROM employees is a SQL statement.
  • #6: Selecting All Columns of All Rows You can display all columns of data in a table by following the SELECT keyword with an asterisk (*). In the example in the slide, the department table contains four columns: DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, and LOCATION_ID. The table contains seven rows, one for each department. You can also display all columns in the table by listing all the columns after the SELECT keyword. For example, the following SQL statement (like the example in the slide) displays all columns and all rows of the DEPARTMENTS table: SELECT department_id, department_name, manager_id, location_idFROM departments;
  • #7: Selecting Specific Columns of All Rows You can use the SELECT statement to display specific columns of the table by specifying the column names, separated by commas. The example in the slide displays all the department numbers and location numbers from the DEPARTMENTS table. In the SELECT clause, specify the columns that you want, in the order in which you want them to appear in the output. For example, to display location before department number going from left to right, you use the following statement: SELECT location_id, department_id FROM departments;
  • #8: Writing SQL Statements Using the following simple rules and guidelines, you can construct valid statements that are both easy to read and easy to edit: SQL statements are not case-sensitive (unless indicated). SQL statements can be entered on one or many lines. •Keywords cannot be split across lines or abbreviated. Clauses are usually placed on separate lines for readability and ease of editing. •Indents should be used to make code more readable. Keywords typically are entered in uppercase; all other words, such as table names and columns, are entered in lowercase. Executing SQL Statements Using iSQL*Plus, click the Execute button to run the command or commands in the editing window. Using SQL*Plus, terminate the SQL statement with a semicolon and then press the Enter key to run the command.
  • #9: Arithmetic Expressions You may need to modify the way in which data is displayed, or you may want to perform calculations or look at what-if scenarios. These are all possible using arithmetic expressions. An arithmetic expression can contain column names, constant numeric values, and the arithmetic operators. Arithmetic Operators The slide lists the arithmetic operators that are available in SQL. You can use arithmetic operators in any clause of a SQL statement (except the FROM clause). Note: With DATE and TIMESTAMP data types, you can use the addition and subtraction operators only.
  • #10: Using Arithmetic Operators The example in the slide uses the addition operator to calculate a salary increase of $300 for all employees. The slide also displays a SALARY+300 column in the output. Note that the resultant calculated column SALARY+300 is not a new column in the EMPLOYEES table; it is for display only. By default, the name of a new column comes from the calculation that generated it—in this case, salary+300. Note: The Oracle server ignores blank spaces before and after the arithmetic operator. Operator Precedence If an arithmetic expression contains more than one operator, multiplication and division are evaluated first. If operators in an expression are of the same priority, then evaluation is done from left to right. You can use parentheses to force the expression that is enclosed by parentheses to be evaluated first. Rules of Precedence: Multiplication and division occur before addition and subtraction. Operators of the same priority are evaluated from left to right. Parentheses are used to override the default precedence or to clarify the statement.
  • #11: Operator Precedence (continued) The first example in the slide displays the last name, salary, and annual compensation of employees. It calculates the annual compensation by multiplying the monthly salary by 12, plus a one-time bonus of $100. Notice that multiplication is performed before addition. Note: Use parentheses to reinforce the standard order of precedence and to improve clarity. For example, the expression in the slide can be written as (12*salary)+100 with no change in the result. Using Parentheses You can override the rules of precedence by using parentheses to specify the desired order in which operators are to be executed. The second example in the slide displays the last name, salary, and annual compensation of employees. It calculates the annual compensation as follows: adding a monthly bonus of $100 to the monthly salary, and then multiplying that subtotal by 12. Because of the parentheses, addition takes priority over multiplication.
  • #12: Column Aliases When displaying the result of a query, iSQL*Plus normally uses the name of the selected column as the column heading. This heading may not be descriptive and hence may be difficult to understand. You can change a column heading by using a column alias. Specify the alias after the column in the SELECT list using a space as a separator. By default, alias headings appear in uppercase. If the alias contains spaces or special characters (such as # or $), or if it is case-sensitive, enclose the alias in double quotation marks (&amp;quot; &amp;quot;).
  • #13: Concatenation Operator You can link columns to other columns, arithmetic expressions, or constant values to create a character expression by using the concatenation operator (||). Columns on either side of the operator are combined to make a single output column. In the example, LAST_NAME and JOB_ID are concatenated, and they are given the alias Employees. Notice that the employee last name and job code are combined to make a single output column. The AS keyword before the alias name makes the SELECT clause easier to read. Null Values with the Concatenation Operator If you concatenate a null value with a character string, the result is a character string. LAST_NAME || NULL results in LAST_NAME.
  • #14: Literal Character Strings A literal is a character, a number, or a date that is included in the SELECT list and that is not a column name or a column alias. It is printed for each row returned. Literal strings of free-format text can be included in the query result and are treated the same as a column in the SELECT list. Date and character literals must be enclosed by single quotation marks (&amp;apos; &amp;apos;); number literals need not be so enclosed.
  • #15: Literal Character Strings (continued) The example in the slide displays last names and job codes of all employees. The column has the heading Employee Details. Notice the spaces between the single quotation marks in the SELECT statement. The spaces improve the readability of the output. In the following example, the last name and salary for each employee are concatenated with a literal to give the returned rows more meaning: SELECT last_name ||&amp;apos;: 1 Month salary = &amp;apos;||salary Monthly FROM employees;
  • #16: SELECT Statement In this lesson, you should have learned how to retrieve data from a database table with the SELECT statement. SELECT *|{[DISTINCT] column [alias],...} FROM table; In the syntax: SELECTis a list of one or more columns * selects all columns DISTINCTsuppresses duplicates column|expressionselects the named column or the expression aliasgives selected columns different headings FROM table specifies the table containing the columns iSQL*Plus iSQL*Plus is an execution environment that you can use to send SQL statements to the database server and to edit and save SQL statements. Statements can be executed from the SQL prompt or from a script file.
  • #19: Limiting Rows Using a Selection In the example in the slide, suppose that you want to display all the employees in department 90. The rows with a value of 90 in the DEPARTMENT_ID column are the only ones that are returned. This method of restriction is the basis of the WHERE clause in SQL.
  • #20: Limiting the Rows That Are Selected You can restrict the rows that are returned from the query by using the WHERE clause. A WHERE clause contains a condition that must be met, and it directly follows the FROM clause. If the condition is true, the row meeting the condition is returned. In the syntax: WHERErestricts the query to rows that meet a condition conditionis composed of column names, expressions, constants, and a comparison operator The WHERE clause can compare values in columns, literal values, arithmetic expressions, or functions. It consists of three elements: Column name Comparison condition Column name, constant, or list of values
  • #21: Using the WHERE Clause In the example, the SELECT statement retrieves the employee ID, name, job ID, and department number of all employees who are in department 90.
  • #22: Character Strings and Dates Character strings and dates in the WHERE clause must be enclosed by single quotation marks (&amp;apos;&amp;apos;). Number constants, however, should not be enclosed by single quotation marks. All character searches are case-sensitive. In the following example, no rows are returned because the EMPLOYEES table stores all the last names in mixed case: SELECT last_name, job_id, department_id FROM employees WHERE last_name = &amp;apos;WHALEN&amp;apos;; Oracle databases store dates in an internal numeric format, representing the century, year, month, day, hours, minutes, and seconds. The default date display is DD-MON-RR. Note: For details about the RR format and about changing the default date format, see the next lesson.
  • #23: Comparison Conditions Comparison conditions are used in conditions that compare one expression to another value or expression. They are used in the WHERE clause in the following format: Syntax ... WHERE expr operator value Example ... WHERE hire_date = &amp;apos;01-JAN-95&amp;apos; ... WHERE salary &amp;gt;= 6000 ... WHERE last_name = &amp;apos;Smith&amp;apos; An alias cannot be used in the WHERE clause. Note: The symbols != and ^= can also represent the not equal to condition.
  • #24: Using Comparison Conditions In the example, the SELECT statement retrieves the last name and salary from the EMPLOYEES table for any employee whose salary is less than or equal to $3,000. Note that there is an explicit value supplied to the WHERE clause. The explicit value of 3000 is compared to the salary value in the SALARY column of the EMPLOYEES table.
  • #25: Using the BETWEEN Condition You can display rows based on a range of values using the BETWEEN range condition. The range that you specify contains a lower limit and an upper limit. The SELECT statement in the slide returns rows from the EMPLOYEES table for any employee whose salary is between $2,500 and $3,500. Values that are specified with the BETWEEN condition are inclusive. You must specify the lower limit first. You can also use the BETWEEN condition on character values: SELECT last_name FROM employees WHERE last_name BETWEEN &amp;apos;King&amp;apos; AND &amp;apos;Smith&amp;apos;;
  • #26: Using the IN Condition To test for values in a specified set of values, use the IN condition. The IN condition is also known as the membership condition. The slide example displays employee numbers, last names, salaries, and manager’s employee numbers for all the employees whose manager’s employee number is 100, 101, or 201. The IN condition can be used with any data type. The following example returns a row from the EMPLOYEES table for any employee whose last name is included in the list of names in the WHERE clause: SELECT employee_id, manager_id, department_id FROM employees WHERE last_name IN (&amp;apos;Hartstein&amp;apos;, &amp;apos;Vargas&amp;apos;); If characters or dates are used in the list, they must be enclosed by single quotation marks (&amp;apos;&amp;apos;).
  • #27: Using the LIKE Condition You may not always know the exact value to search for. You can select rows that match a character pattern by using the LIKE condition. The character pattern–matching operation is referred to as a wildcard search. Two symbols can be used to construct the search string. The SELECT statement in the slide returns the employee first name from the EMPLOYEES table for any employee whose first name begins with the letter S. Note the uppercase S. Names beginning with an s are not returned. The LIKE condition can be used as a shortcut for some BETWEEN comparisons. The following example displays the last names and hire dates of all employees who joined between January 1995 and December 1995: SELECT last_name, hire_date FROM employees WHERE hire_date LIKE &amp;apos;%95&amp;apos;;
  • #28: Combining Wildcard Characters The % and _ symbols can be used in any combination with literal characters. The example in the slide displays the names of all employees whose last names have the letter o as the second character. ESCAPE Option When you need to have an exact match for the actual % and _ characters, use the ESCAPE option. This option specifies what the escape character is. If you want to search for strings that contain ‘SA_’, you can use the following SQL statement: SELECT employee_id, last_name, job_id FROM employees WHERE job_id LIKE &amp;apos;%SA\_%&amp;apos; ESCAPE &amp;apos;\&amp;apos;; The ESCAPE option identifies the backslash (\) as the escape character. In the pattern, the escape character precedes the underscore (_). This causes the Oracle Server to interpret the underscore literally.
  • #29: Using the NULL Conditions The NULL conditions include the IS NULL condition and the IS NOT NULL condition. The IS NULL condition tests for nulls. A null value means the value is unavailable, unassigned, unknown, or inapplicable. Therefore, you cannot test with = because a null cannot be equal or unequal to any value. The slide example retrieves the last names and managers of all employees who do not have a manager. Here is another example: To display last name, job ID, and commission for all employees who are not entitled to receive a commission, use the following SQL statement: SELECT last_name, job_id, commission_pct FROM employees WHERE commission_pct IS NULL;
  • #30: Logical Conditions A logical condition combines the result of two component conditions to produce a single result based on those conditions, or it inverts the result of a single condition. A row is returned only if the overall result of the condition is true. Three logical operators are available in SQL: AND OR NOT All the examples so far have specified only one condition in the WHERE clause. You can use several conditions in one WHERE clause using the AND and OR operators.
  • #31: Using the AND Operator In the example, both conditions must be true for any record to be selected. Therefore, only employees who have a job title that contains the string ‘MAN’ and earn $10,000 or more are selected. All character searches are case-sensitive. No rows are returned if ‘MAN’ is not uppercase. Character strings must be enclosed by quotation marks. AND Truth Table The following table shows the results of combining two expressions with AND:
  • #32: Using the OR Operator In the example, either condition can be true for any record to be selected. Therefore, any employee who has a job ID that contains the string ‘MAN’ or earns $10,000 or more is selected. OR Truth Table The following table shows the results of combining two expressions with OR:
  • #33: Using the NOT Operator The slide example displays the last name and job ID of all employees whose job ID is not IT_PROG, ST_CLERK, or SA_REP. NOT Truth Table The following table shows the result of applying the NOT operator to a condition: Note: The NOT operator can also be used with other SQL operators, such as BETWEEN, LIKE, and NULL. ... WHERE job_id NOT IN (&amp;apos;AC_ACCOUNT&amp;apos;, &amp;apos;AD_VP&amp;apos;) ... WHERE salary NOT BETWEEN 10000 AND 15000 ... WHERE last_name NOT LIKE &amp;apos;%A%&amp;apos; ... WHERE commission_pct IS NOT NULL
  • #34: Rules of Precedence The rules of precedence determine the order in which expressions are evaluated and calculated. The table lists the default order of precedence. You can override the default order by using parentheses around the expressions that you want to calculate first.
  • #35: Summary In this lesson, you should have learned about restricting and sorting rows that are returned by the SELECT statement. You should also have learned how to implement various operators and conditions. By using the iSQL*Plus substitution variables, you can add flexibility to your SQL statements. You can query users at run time and enable them to specify criteria.