SQL
Statements
INSTRUCTOR:
ELLEN GRACE PORRAS
2
Table Name: Students
Student_ID Firstname Lastname Age Allowance
1100 Fernan Cabuso 22 400
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
1103 Andrea Laki 20 600
Table Name: Students
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_ID 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
Output
ID
1100
1101
1102
1103
Example: Alias
SELECT Student_ID AS ID
FROM Students
6
Table Name: Students
Student_ID 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
7
Output
ID Pangalan
1100 Fernan
1101 Joshua
1102 Joyce
1103 Andrea
Example: Alias
Select Student_ID as ID, Firstname as Pangalan
from Employees
8
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!
9
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:
10
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:
11
Table Name: Students
Student_ID 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%’;
12
Output
Student_ID Firstname Lastname Age Allowance
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
Example: Like
SELECT *
FROM Students
WHERE FirstName LIKE ‘J%’;
13
The following SQL statement selects all records in Students Table with
a LastName ending with “s". We use the following LIKE statement:
14
Table Name: Students
Student_ID 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’;
15
Output
Student_ID Firstname Lastname Age Allowance
1101 Joshua Guimaras 22 500
Example: Like
SELECT *
FROM Students
WHERE Lastname LIKE ‘%s’;
16
The following SQL statement selects all records in Students
Table with Firstname that have “Jo” in any position. We use
the following LIKE statement:
17
Table Name: Students
Student_ID 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 Firstname LIKE ‘%Jo%’;
18
Output
Student_ID Firstname Lastname Age Allowance
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
Example: Like
SELECT *
FROM Persons
WHERE Firstname LIKE ‘%Jo%’;
19
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:
20
Table Name: Students
Student_ID 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%’;
21
Output
Student_ID Firstname Lastname Age Allowance
1101 Joshua Guimaras 22 500
1102 Joyce Hisona 21 500
Example: Like
SELECT *
FROM Students
WHERE FirstName LIKE ‘_O%’;
22
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:
23
Table Name: Students
Student_ID 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’;
24
Output
Student_ID Firstname Lastname Age Allowance
1101 Joshua Guimaras 22 500
Example: Like
SELECT *
FROM Students
WHERE FirstName LIKE ‘j%a’;
25
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:
26
Table Name: Students
Student_ID 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%’;
27
Output
Student_ID Firstname Lastname Age Allowance
1100 Fernan Cabuso 22 400
1103 Andrea Laki 20 600
Example: Like
SELECT *
FROM Students
WHERE FirstName NOT LIKE ‘j%’;
28
Thank you!

Information Management for BSIT Students.pptx

  • 1.
  • 2.
    2 Table Name: Students Student_IDFirstname Lastname Age Allowance 1100 Fernan Cabuso 22 400 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 1103 Andrea Laki 20 600 Table Name: Students
  • 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_IDFirstname 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.
  • 6.
    6 Table Name: Students Student_IDFirstname 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
  • 7.
    7 Output ID Pangalan 1100 Fernan 1101Joshua 1102 Joyce 1103 Andrea Example: Alias Select Student_ID as ID, Firstname as Pangalan from Employees
  • 8.
    8 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!
  • 9.
    9 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:
  • 10.
    10 Example: LIKE Operator Thefollowing SQL statement selects all records in Students Table with a FirstName starting with “J". We use the following LIKE statement:
  • 11.
    11 Table Name: Students Student_IDFirstname 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%’;
  • 12.
    12 Output Student_ID Firstname LastnameAge Allowance 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 Example: Like SELECT * FROM Students WHERE FirstName LIKE ‘J%’;
  • 13.
    13 The following SQLstatement selects all records in Students Table with a LastName ending with “s". We use the following LIKE statement:
  • 14.
    14 Table Name: Students Student_IDFirstname 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’;
  • 15.
    15 Output Student_ID Firstname LastnameAge Allowance 1101 Joshua Guimaras 22 500 Example: Like SELECT * FROM Students WHERE Lastname LIKE ‘%s’;
  • 16.
    16 The following SQLstatement selects all records in Students Table with Firstname that have “Jo” in any position. We use the following LIKE statement:
  • 17.
    17 Table Name: Students Student_IDFirstname 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 Firstname LIKE ‘%Jo%’;
  • 18.
    18 Output Student_ID Firstname LastnameAge Allowance 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 Example: Like SELECT * FROM Persons WHERE Firstname LIKE ‘%Jo%’;
  • 19.
    19 The following SQLstatement selects all records in Students Table with a Firstname that have “O” in the second position. We use the following LIKE statement:
  • 20.
    20 Table Name: Students Student_IDFirstname 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%’;
  • 21.
    21 Output Student_ID Firstname LastnameAge Allowance 1101 Joshua Guimaras 22 500 1102 Joyce Hisona 21 500 Example: Like SELECT * FROM Students WHERE FirstName LIKE ‘_O%’;
  • 22.
    22 The following SQLstatement selects all records in Students Table with a Firstname that starts with “j” and ends with “a”. We use the following LIKE statement:
  • 23.
    23 Table Name: Students Student_IDFirstname 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’;
  • 24.
    24 Output Student_ID Firstname LastnameAge Allowance 1101 Joshua Guimaras 22 500 Example: Like SELECT * FROM Students WHERE FirstName LIKE ‘j%a’;
  • 25.
    25 The following SQLstatement selects all records in Students Table with a Firstname that does not starts with “j”. We use the following LIKE statement:
  • 26.
    26 Table Name: Students Student_IDFirstname 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%’;
  • 27.
    27 Output Student_ID Firstname LastnameAge Allowance 1100 Fernan Cabuso 22 400 1103 Andrea Laki 20 600 Example: Like SELECT * FROM Students WHERE FirstName NOT LIKE ‘j%’;
  • 28.