SQL Statements
INSTRUCTOR:
ELLEN GRACE
PORRAS
SECOND SEMESTER 2022-2023
2
Table Name: Students
Student_I
D
Firstname Lastname Age Allowance
1100 Fernan Cabuso 22 400
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
1103 Andrea Laki 20 600
3
SQL Aliases
SQL aliases are used to give a table, or a column in a table,
a temporary name. Aliases are often used to make column
names more readable. An alias only exists for the duration
of that query. An alias is created with the AS keyword.
4
Table Name: Students
Student_I
D
Firstname Lastname Age Allowance
1100 Fernan Cabuso 22 400
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
1103 Andrea Laki 20 600
Example: Alias
SELECT Student_ID AS ID
FROM Students
5
Table Name: Students
Student_I
D
Firstname Lastname Age Allowance
1100 Fernan Cabuso 22 400
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
1103 Andrea Laki 20 600
Example: Alias
Select Student_ID as ID, Firstname as Pangalan
from Employees
6
The SQL LIKE Operator
The LIKE operator is used in a WHERE clause to search for a
specified pattern in a column.
There are two wildcards often used in conjunction with
the LIKE operator:
• The percent sign (%) represents zero, one, or multiple characters
• The underscore sign (_) represents one, single character
• The percent sign and the underscore can also be used in
combinations!
7
LIKE Operator Description
WHERE Student_Name LIKE 'a%' Finds any values that start with "a"
WHERE Student_Name LIKE '%a' Finds any values that end with "a"
WHERE Student_Name LIKE
'%or%'
Finds any values that have "or" in any position
WHERE Student_Name LIKE '_r%' Finds any values that have "r" in the second position
WHERE Student_Name LIKE 'a_%'
Finds any values that start with "a" and are at least 2
characters in length
WHERE Student_Name LIKE
'a__%'
Finds any values that start with "a" and are at least 3
characters in length
WHERE Student_Name LIKE 'a%o' Finds any values that start with "a" and ends with "o"
Here are some examples showing different LIKE operators with '%' and '_' wildcards:
8
Example: LIKE Operator
 The following SQL statement selects all records in
Students Table with a FirstName starting with “J".
We use the following LIKE statement:
9
Table Name: Students
Student_I
D
Firstname Lastname Age Allowance
1100 Fernan Cabuso 22 400
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
1103 Andrea Laki 20 600
Example: Like
SELECT *
FROM Students
WHERE FirstName LIKE ‘J%’;
10
 The following SQL statement selects all
records in Students Table with a LastName
ending with “s". We use the following LIKE
statement:
11
Table Name: Students
Student_I
D
Firstname Lastname Age Allowance
1100 Fernan Cabuso 22 400
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
1103 Andrea Laki 20 600
Example: Like
SELECT *
FROM Students
WHERE Lastname LIKE ‘%s’;
12
 The following SQL statement selects all
records in Students Table with Firstname
that have “Jo” in any position. We use the
following LIKE statement:
13
Table Name: Students
Student_I
D
Firstname Lastname Age Allowance
1100 Fernan Cabuso 22 400
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
1103 Andrea Laki 20 600
Example: Like
SELECT *
FROM Persons
WHERE City LIKE ‘%Jo%’;
14
 The following SQL statement selects all
records in Students Table with a Firstname that
have “O” in the second position. We use the
following LIKE statement:
15
Table Name: Students
Student_I
D
Firstname Lastname Age Allowance
1100 Fernan Cabuso 22 400
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
1103 Andrea Laki 20 600
Example: Like
SELECT *
FROM Students
WHERE FirstName LIKE ‘_O%’;
16
 The following SQL statement selects all
records in Students Table with a Firstname
that starts with “j” and ends with “a”. We
use the following LIKE statement:
17
Table Name: Students
Student_I
D
Firstname Lastname Age Allowance
1100 Fernan Cabuso 22 400
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
1103 Andrea Laki 20 600
Example: Like
SELECT *
FROM Students
WHERE FirstName LIKE ‘j%a’;
18
The following SQL statement selects
all records in Students Table with a
Firstname that does not starts with “j”.
We use the following LIKE statement:
19
Table Name: Students
Student_I
D
Firstname Lastname Age Allowance
1100 Fernan Cabuso 22 400
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
1103 Andrea Laki 20 600
Example: Like
SELECT *
FROM Students
WHERE FirstName NOT LIKE ‘j%’;

SQL Statements.pptx

  • 1.
  • 2.
    2 Table Name: Students Student_I D FirstnameLastname Age Allowance 1100 Fernan Cabuso 22 400 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 1103 Andrea Laki 20 600
  • 3.
    3 SQL Aliases SQL aliasesare used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable. An alias only exists for the duration of that query. An alias is created with the AS keyword.
  • 4.
    4 Table Name: Students Student_I D FirstnameLastname Age Allowance 1100 Fernan Cabuso 22 400 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 1103 Andrea Laki 20 600 Example: Alias SELECT Student_ID AS ID FROM Students
  • 5.
    5 Table Name: Students Student_I D FirstnameLastname Age Allowance 1100 Fernan Cabuso 22 400 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 1103 Andrea Laki 20 600 Example: Alias Select Student_ID as ID, Firstname as Pangalan from Employees
  • 6.
    6 The SQL LIKEOperator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: • The percent sign (%) represents zero, one, or multiple characters • The underscore sign (_) represents one, single character • The percent sign and the underscore can also be used in combinations!
  • 7.
    7 LIKE Operator Description WHEREStudent_Name LIKE 'a%' Finds any values that start with "a" WHERE Student_Name LIKE '%a' Finds any values that end with "a" WHERE Student_Name LIKE '%or%' Finds any values that have "or" in any position WHERE Student_Name LIKE '_r%' Finds any values that have "r" in the second position WHERE Student_Name LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length WHERE Student_Name LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length WHERE Student_Name LIKE 'a%o' Finds any values that start with "a" and ends with "o" Here are some examples showing different LIKE operators with '%' and '_' wildcards:
  • 8.
    8 Example: LIKE Operator The following SQL statement selects all records in Students Table with a FirstName starting with “J". We use the following LIKE statement:
  • 9.
    9 Table Name: Students Student_I D FirstnameLastname Age Allowance 1100 Fernan Cabuso 22 400 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 1103 Andrea Laki 20 600 Example: Like SELECT * FROM Students WHERE FirstName LIKE ‘J%’;
  • 10.
    10  The followingSQL statement selects all records in Students Table with a LastName ending with “s". We use the following LIKE statement:
  • 11.
    11 Table Name: Students Student_I D FirstnameLastname Age Allowance 1100 Fernan Cabuso 22 400 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 1103 Andrea Laki 20 600 Example: Like SELECT * FROM Students WHERE Lastname LIKE ‘%s’;
  • 12.
    12  The followingSQL statement selects all records in Students Table with Firstname that have “Jo” in any position. We use the following LIKE statement:
  • 13.
    13 Table Name: Students Student_I D FirstnameLastname Age Allowance 1100 Fernan Cabuso 22 400 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 1103 Andrea Laki 20 600 Example: Like SELECT * FROM Persons WHERE City LIKE ‘%Jo%’;
  • 14.
    14  The followingSQL statement selects all records in Students Table with a Firstname that have “O” in the second position. We use the following LIKE statement:
  • 15.
    15 Table Name: Students Student_I D FirstnameLastname Age Allowance 1100 Fernan Cabuso 22 400 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 1103 Andrea Laki 20 600 Example: Like SELECT * FROM Students WHERE FirstName LIKE ‘_O%’;
  • 16.
    16  The followingSQL statement selects all records in Students Table with a Firstname that starts with “j” and ends with “a”. We use the following LIKE statement:
  • 17.
    17 Table Name: Students Student_I D FirstnameLastname Age Allowance 1100 Fernan Cabuso 22 400 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 1103 Andrea Laki 20 600 Example: Like SELECT * FROM Students WHERE FirstName LIKE ‘j%a’;
  • 18.
    18 The following SQLstatement selects all records in Students Table with a Firstname that does not starts with “j”. We use the following LIKE statement:
  • 19.
    19 Table Name: Students Student_I D FirstnameLastname Age Allowance 1100 Fernan Cabuso 22 400 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 1103 Andrea Laki 20 600 Example: Like SELECT * FROM Students WHERE FirstName NOT LIKE ‘j%’;