PCIT - 02 Chapter 5 of Fundamental of Database System
PCIT - 02 Chapter 5 of Fundamental of Database System
System
Chapter 5
MORE SQL
EMILVERCHRISTIAN V. ARQUERO
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.
This query will retrieve all columns from the "tbl_customer" table
where the Customer ID is 12345.
This query will retrieve all columns from the "tbl_customer" table
where the customer name is 'John Smith'.
This query will retrieve all columns from the "tbl_customer" table
where the customer birthday is after January 1, 2000.
This query will retrieve all columns from the "tbl_customer" table, and
it will return every row present in the table, without any restrictions.
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.
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.
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'.
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.
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.
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.
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.
1. Column Alias:
2. Table Alias:
It's important to note that using the AS keyword for column and table
aliases is optional in many database systems.
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.
This query retrieves the order ID and customer name from the
"orders" and "customers" tables, respectively, for the records where
the customer IDs match.
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.
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.
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.