0% found this document useful (0 votes)
44 views48 pages

PCIT - 02 Chapter 5 of Fundamental of Database System

Documents for IT student

Uploaded by

kaye layan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views48 pages

PCIT - 02 Chapter 5 of Fundamental of Database System

Documents for IT student

Uploaded by

kaye layan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Fundamental of Database

System
Chapter 5
MORE SQL
EMILVERCHRISTIAN V. ARQUERO

CENTRAL PHILIPPINES STATE UNIVERSITY


Chapter 5:
MORE SQL
• SQL WHERE
• SQL AND
• SQL OR
• SQL AS
• SQL WITH
• Order BY
• SQL JOIN
• SQL String Functions
EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM
What is SQL WHERE?
The SQL WHERE statement is a clause used in a SQL (Structured Query
Language) query to filter and retrieve specific rows from a database
table based on specified conditions. It allows you to define conditions
that the data must meet to be included in the query result.

The WHERE statement is typically used in conjunction with the SELECT


statement, although it can also be used with other statements such as
UPDATE and DELETE.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL WHERE
The general syntax of a SELECT statement with a WHERE clause is as
follows:

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL WHERE
In the previous syntax:

1. SELECT specifies the columns you want to retrieve from the table.
2. FROM specifies the table from which you want to retrieve the data.
3. WHERE specifies the conditions that must be met for a row to be
included in the result set.
4. CONDITION is a logical expression that evaluates to either true or
false. It can consist of column comparisons, logical operators (AND,
OR), and other SQL functions or expressions.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL WHERE
Here are a few examples of SQL queries using the WHERE statement
with the "tbl_customer" table:

Example 1: Retrieve a customer with


a specific Customer ID:

This query will retrieve all columns from the "tbl_customer" table
where the Customer ID is 12345.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL WHERE
Here are a few examples of SQL queries using the WHERE statement
with the "tbl_customer" table:

Example 2: Retrieve customers


with a specific customer name:

This query will retrieve all columns from the "tbl_customer" table
where the customer name is 'John Smith'.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL WHERE
Here are a few examples of SQL queries using the WHERE statement
with the "tbl_customer" table:

Example 3: Retrieve customers


born after a specific date:

This query will retrieve all columns from the "tbl_customer" table
where the customer birthday is after January 1, 2000.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL WHERE
If you do not use the WHERE statement in an SQL query, the query will
retrieve all rows from the specified table without any filtering or
conditions. In other words, it will return the entire contents of the
table.

For example, consider the following


query without a WHERE statement:

This query will retrieve all columns from the "tbl_customer" table, and
it will return every row present in the table, without any restrictions.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


What is SQL AND?
In SQL, the AND operator is a logical operator used to combine
multiple conditions in a WHERE clause. It allows you to create more
complex conditions by specifying that all conditions must be true for a
row to be included in the query result.
The general syntax for using the AND operator is as follows:

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL AND
In the previous syntax:

1. column1, column2, ... specifies the columns you want to retrieve


from the table.
2. table_name specifies the table from which you want to retrieve the
data.
3. condition1, condition2, ... are the individual conditions that must
be true for a row to be included in the result set. These conditions
can be column comparisons, logical operators, SQL functions, or
expressions.
EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM
SQL AND
Example: Let's say we want to retrieve all customers whose name is "John" and whose
birthday is after January 1, 2000. We can use the AND operator to combine these
conditions:

This query will retrieve all columns from the "tbl_customer" table where the customer
name is 'John' AND the customer birthday is after January 1, 2000.
The first condition cus_name = 'John' checks if the customer name is exactly 'John'.
The second condition cus_bday > '2000-01-01' checks if the customer birthday is greater
than January 1, 2000.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


What is SQL OR?
In SQL, the OR operator is a logical operator used to combine multiple
conditions in a WHERE clause. It allows you to create more complex
conditions by specifying that at least one of the conditions must be
true for a row to be included in the query result.

The general syntax for using the OR operator is as follows:

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL OR
In the previous syntax:

1. column1, column2, ... specifies the columns you want to retrieve


from the table.
2. table_name specifies the table from which you want to retrieve the
data.
3. condition1, condition2, ... are the individual conditions that must be
true for a row to be included in the result set. These conditions can
be column comparisons, logical operators, SQL functions, or
expressions.
EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM
SQL OR
Here's an example to illustrate the usage of the OR operator:

This query retrieves all columns from the "tbl_customer" table where the
customer name is either 'John Smith' OR 'Jane Doe'. If a row has a customer
name matching either condition, it will be included in the query result.
The OR operator allows you to combine multiple conditions to create more
flexible queries. By using OR, you can define different filtering criteria and
retrieve data that satisfies at least one of the specified conditions.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL OR
Example 2. Let's say we want to retrieve all customers whose name is either 'John' or 'Jane' and
whose address contains the word 'Street'. We can use the OR operator to combine the name
conditions and the AND operator to combine the address condition:

This query will retrieve all columns (*) from the "tbl_customer" table where the customer name is
either 'John' OR 'Jane' AND the customer address contains the word 'Street’.
The first condition (cus_name = 'John' OR cus_name = 'Jane') checks if the customer name is either
'John' or 'Jane’.
The second condition cus_addrs LIKE '%Street%' checks if the customer address contains the word
'Street'. The % wildcard matches any characters before or after 'Street'.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


Percent sign (%) wildcard
The percent sign (%) wildcard is used in conjunction with the LIKE
operator in SQL to match any sequence of characters (including zero
characters) in a string.

Here are the key details on how to use the percent (%) wildcard:
1. Matches any sequence of characters (including zero characters) in
the specified position.
2. It can be used at the beginning, end, or in the middle of a string.
3. The percent sign can be used multiple times in a pattern.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


Percent sign (%) wildcard
Examples:
1. LIKE '%apple%' matches any string that contains the word 'apple' anywhere
within it.
2. LIKE 'a%' matches any string that starts with the letter 'a'.
3. LIKE '%e' matches any string that ends with the letter 'e'.
4. LIKE 'a%p%e' matches strings like 'apple', 'ape', 'applepie', etc.

The percent (%) wildcard is powerful for performing pattern matching in SQL
queries. By placing it in appropriate positions within the pattern, you can search
for strings that contain specific substrings or follow certain patterns.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


What is SQL ORDER BY?
In SQL, the ORDER BY clause is used to sort the result set of a query
based on one or more columns. It allows you to specify the column(s)
by which you want to sort the data and the order in which you want
the sorting to be applied.
The basic syntax of the ORDER BY clause is as follows:

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL ORDER BY
In the previous syntax:

1. column1, column2, ... specifies the columns you want to retrieve


from the table.
2. table_name specifies the table from which you want to retrieve the
data.
3. column1, column2, ... after the ORDER BY keyword specify the
columns by which you want to sort the result set.
4. ASC (Ascending) or DESC (Descending) specifies the order of sorting.
ASC is the default if not specified.
EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM
SQL ORDER BY
Here are a few examples to illustrate the usage of the ORDER BY
clause:
Example 1. Sorting by a
Single Column:

This query retrieves the customer name and order date from the "orders" table
and sorts the result set in descending order based on the order date.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL ORDER BY
Here are a few examples to illustrate the usage of the ORDER BY
clause:
Example 2. Sorting by
Multiple Columns:

This query retrieves the product name, unit price, and units in stock from the
"products" table and sorts the result set first by units in stock in descending order
and then by unit price in ascending order.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL ORDER BY
Here are a few examples to illustrate the usage of the ORDER BY
clause:
Example 3. Let's say you want to
retrieve all columns from the "tbl_customer“
table and sort the result set based on the customer
names in ascending order.
This query selects all columns from the "tbl_customer" table and applies sorting
based on the customer names in ascending order using the ORDER BY clause. This
query selects all columns (*) from the "tbl_customer" table and applies sorting
based on the customer names in ascending order using the ORDER BY clause.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL ORDER BY
The ORDER BY clause is helpful for arranging query results in a specific
order based on column values. It can be used with various data types,
including numbers, dates, and strings. By specifying the appropriate
column(s) and sorting order, you can control the presentation and
analysis of the query results in SQL.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


What is SQL AS?
In SQL, the AS keyword is used to assign an alias or alternative name to
a column or table in a query. It provides a way to rename the result of
a column or table temporarily within the query, making it easier to
reference and manipulate the data.

The AS keyword is typically used in conjunction with the SELECT


statement to create column aliases or with the FROM clause to create
table aliases.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL AS
Here are examples of how AS is used in SQL:

1. Column Alias:

2. Table Alias:

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL AS
Using aliases can improve the readability and conciseness of SQL
queries, especially when dealing with complex queries involving
multiple tables or long column names. Aliases also allow you to refer
to the columns or tables by a more meaningful or shorter name within
the query.

It's important to note that using the AS keyword for column and table
aliases is optional in many database systems.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL AS
Example 1. Column Alias:

In this example, the column aliases customer_name and


customer_address are assigned to the cus_name and cus_addrs
columns, respectively. The result set will display the column values
with these aliases.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL AS
Example 2. Table Alias:

Here, the table alias c is assigned to the "tbl_customer" table. The


column names are prefixed with the table alias in the query to specify
which table the columns belong to. Additionally, the query joins the
"tbl_customer" table with the "tbl_orders" table using the common
cus_id column.
EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM
What is SQL WITH?
In SQL, the WITH clause, also known as the Common Table Expression
(CTE), is used to create temporary named result sets within a query. It
allows you to define a subquery and reference it multiple times within
the main query, improving code readability and maintainability.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL WITH
Here's a breakdown of the elements in the syntax:
1. cte_name: The name given to the Common Table Expression (CTE).
2. (column1, column2, ...): Optional column list specifying the column names for
the CTE. This is useful when you want to explicitly name the columns in the CTE.
3. AS: Keyword to introduce the CTE definition.
4. SELECT column1, column2, ...: The subquery that defines the CTE. It can include
any valid SQL statement, including joins, aggregations, and filtering conditions.
5. FROM table_name: The table or tables involved in the subquery.
6. WHERE condition: Optional filtering condition applied to the subquery.
7. SELECT ... FROM ...: The main query that references the CTE.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL WITH
The WITH clause is commonly used in complex queries when you need
to break down a complex problem into smaller, more manageable
parts. It allows you to create temporary result sets (CTEs) that can be
referenced and reused multiple times within the main query,
simplifying the overall query structure and improving query
performance.

The WITH clause enhances the readability and maintainability of SQL


queries by allowing you to define and reference temporary result sets
within the query itself.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL WITH
Here's a simple
example to illustrate
the usage of the
WITH clause:

In this example, the CTE named "sales" retrieves the product name, unit price, and
quantity from the "orders" and "order_details" tables. Then, the main query calculates the
total sales for each product by aggregating the sales data from the CTE. The CTE helps
simplify the main query by encapsulating the sales logic.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


What is SQL JOIN?
In SQL, the JOIN operation is used to combine rows from two or more
tables based on a related column between them. It allows you to
retrieve data from multiple tables simultaneously by specifying how
the tables are related.

There are different types of JOIN operations:


1. INNER JOIN
2. LEFT JOIN (or LEFT OUTER JOIN)
3. RIGHT JOIN (or RIGHT OUTER JOIN)
4. FULL JOIN (or FULL OUTER JOIN)
EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM
INNER JOIN
Retrieves records that have matching values in both tables being
joined.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


INNER JOIN
Example

This query retrieves the order ID and customer name from the
"orders" and "customers" tables, respectively, for the records where
the customer IDs match.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


LEFT JOIN (or LEFT OUTER JOIN)
Retrieves all records from the left (first) table and matching records
from the right (second) table. If there are no matches, the result will
contain NULL values for the right table.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


LEFT JOIN (or LEFT OUTER JOIN)
Example

This query retrieves the customer name from the "customers" table
and the order date from the "orders" table. All customers will be
included, and for those who have placed orders, the order date will be
displayed. For customers without orders, the order date will be NULL.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


RIGHT JOIN (or RIGHT OUTER JOIN)
Retrieves all records from the right (second) table and matching
records from the left (first) table. If there are no matches, the result
will contain NULL values for the left table.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


RIGHT JOIN (or RIGHT OUTER JOIN)
Example

This query retrieves the customer name from the "customers" table and
the order date from the "orders" table. All orders will be included, and
for those with matching customers, the customer name will be
displayed. For orders without customers, the customer name will be
NULL.
EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM
FULL JOIN (or FULL OUTER JOIN)
Retrieves all records when there is a match in either the left (first)
table or the right (second) table. If there are no matches, NULL values
will be included.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


FULL JOIN (or FULL OUTER JOIN)
Example

This query retrieves the customer name from the "customers" table
and the order date from the "orders" table. All customers and orders
will be included, and if there are matches, the corresponding data will
be displayed. For unmatched records, NULL values will be included.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL JOIN
JOIN operations allow you to combine data from multiple tables based
on their relationships. By specifying the appropriate JOIN type and the
join condition, you can retrieve the desired data by merging related
records from different tables.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


What is SQL String Functions?
SQL string functions are built-in functions provided by SQL databases
to manipulate and perform operations on character or string values.
These functions allow you to modify, combine, extract, and format
strings within SQL queries.
Here are some commonly used SQL string functions:
1. CONCAT(): Concatenates two or more strings together.
Example: SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM
employees

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL String Functions
2. LENGTH(): Returns the length (number of characters) of a string.
Example: SELECT LENGTH(product_name) AS name_length FROM products
3. UPPER(): Converts a string to uppercase.
Example: SELECT UPPER(last_name) AS last_name_upper FROM employees
4. LOWER(): Converts a string to lowercase.
Example: SELECT LOWER(first_name) AS first_name_lower FROM employees
5. SUBSTRING(): Extracts a substring from a string based on a specified starting
position and length.
Example: SELECT SUBSTRING(product_name, 1, 5) AS short_name FROM products

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL String Functions
6. TRIM(): Removes leading and trailing spaces from a string.
Example: SELECT TRIM(product_name) AS trimmed_name FROM products
7. REPLACE(): Replaces occurrences of a specified substring within a string with
another substring.
Example: SELECT REPLACE(product_description, 'old', 'new') AS
updated_description FROM products
8. CHARINDEX() or INSTR(): Returns the starting position of a substring within a
string.
Example: SELECT CHARINDEX('world', 'Hello world') AS position FROM dual

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


SQL String Functions
9. LEFT() or SUBSTR(): Returns a specified number of characters from the beginning
of a string.
Example: SELECT LEFT(product_name, 3) AS short_name FROM products
10. RIGHT(): Returns a specified number of characters from the end of a string.
Example: SELECT RIGHT(product_name, 3) AS last_chars FROM products

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM


These are just a few examples of SQL string functions.
Different databases may have slight variations in function
names or syntax, so it's always recommended to refer to the
specific documentation of your database system for a
complete list of available string functions and their usage.

EMILVERCHISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM

You might also like