Create calculated fields
splice
SELECT Concat(vend_name, ' (', vend_country, ')')
AS ven_title
FROM Vendors
ORDER BY vend_name;
Compute
SELECT prod_id, quantity, item_price,
quantity*item_price AS expanded_price
FROM OrderItems
WHERE order_num = 20008;
Test calculations
SELECT
Can be used to verify the statement is correct or test, for example:
SELECT 2 * 3; -- (will return 6)
SELECT TRIM(' abc '); -- (will return 'abc')
SELECT Now(); -- (will return current time)
Data handler
function
Text handling functions
SELECT vend_name, UPPER(vend_name) AS vend_name_upcase
FROM Vendors
ORDER BY vend_name;
SOUNDEX()
Function, a text string into any algorithm which describes a syllable represented by alphanumeric mode, may be used to look fuzzy sound
SELECT cust_name, cust_contact
FROM Customers
WHERE SOUNDEX(cust_contact) = SOUNDEX('Michael Green');
Date and time handling functions
SELECT order_num
FROM Orders
WHERE strftime('%Y', order_date) = 2012
Numerical handler
General mathematical functions ABS() COS() EXP() PI() SIN() SQRT() TAN()
are absolute value, cosine, exponential, pi, sine, square root, tangent
Aggregated data
Aggregate Functions
AVG
SELECT AVG(prod_price) AS avg_price
FROM Products
WHERE vend_id = 'DLL01';
COUNT
SELECT COUNT(*) AS num_cust
FROM Customers;
MAX
SELECT MAX(prod_price) AS max_price
FROM Products;
MIN
SELECT MIN(prod_price) AS min_price
FROM Products;
SUM
SELECT SUM(quantity) AS items_ordered
FROM OrderItems
WHERE order_num = 20005;
Aggregation of different values
DISTINCT
SELECT AVG(DISTINCT prod_price) AS avg_price
FROM Products
WHERE vend_id = 'DLL01';
A combination of aggregate functions
SELECT COUNT(*) AS num_items,
MIN(prod_price) AS price_min,
MAX(prod_price) AS price_max,
AVG(prod_price) AS price_avg
FROM Products;
Packet data
GROUP BY
Creating groups
SELECT vend_id, COUNT(*) AS num_prods
FROM Products
GROUP BY vend_id;
GROUP BY
In the WHERE
following statement, ORDER BY
before the statement
Packet filtering
WHERE
Line filter, HAVING
the filter packet
SELECT cust_id, COUNT(*) AS orders
FROM Orders
GROUP BY cust_id
HAVING COUNT(*) >= 2;
SELECT clause order
SELECT -> FROM -> WHERE -> GROUP BY -> HAVING ->ORDER BY
SELECT vend_id, COUNT(*) AS num_prods
FROM Products
WHERE prod_price >= 4
GROUP BY vend_id
HAVING COUNT(*) >= 2;
ORDER BY vend_id
Using Subqueries
Subqueries
Sub-query filter
Outwardly from the process
SELECT cust_name, cust_contact
FROM Customers
WHERE cust_id IN (SELECT cust_id
FROM Orders
WHERE order_num IN (SELECT order_num
FROM ORDERItems
WHERE prod_id = 'RGAN01'));
As calculated field
SELECT cust_name, cust_state,
(SELECT COUNT(*)
FROM Orders
WHERE Orders.cust_id = Customers.cust_id) AS orders
FROM Customers
ORDER BY cust_name;
Joining tables
coupling
Creating links
SELECT vend_name, prod_name, prod_price
FROM Vendors, Products
WHERE Vendors.vend_id = Products.vend_id;
Cartesian product called cross coupling, not coupled condition, directly multiplying two tables
The inner coupling
SELECT vend_name, prod_name, prod_price
FROM Vendors INNER JOIN Products
ON Vendors.vend_id = Products.vend_id;
Inner coupling known equivalent coupler , two tables based on equality tests
Join multiple tables
SELECT prod_name, vend_name, prod_price, quantity
FROM OrderItems, Products, Vendors
WHERE Products.vend_id = Vendors.vend_id
AND OrderItems.prod_id = Products.prod_id
AND order_num = 20007;
Advanced link
Table aliases
Benefits can shorten the sentence, allowing many times the same table in a SQL statement using
SELECT cust_name, cust_contact
FROM Customers AS C, Orders AS O, OrderItems AS OI
WHERE C.cust_id = O.cust_id
AND O.order_num = OI.order_num
AND OI.prod_id = 'RGAN01';
Self-join
SELECT C2.cust_id, C2.cust_name, C2.cust_contact
FROM Customers AS C1, Customers AS C2
WHERE C2.cust_name = C1.cust_name
AND C1.cust_contact = 'Jim Jones';
DBMS processing link is far faster than subqueries
Natural links
Exclude multiple times, once for each column returned
External link
SELECT Customers.cust_id, Orders.order_num
FROM Customers LEFT OUTER JOIN Orders
ON Customers.cust_id = Orders.cust_id;
With a link aggregate function
SELECT Customers.cust_id, COUNT(Orders.order_num) AS num_order
FROM Customers INNER JOIN Orders
ON Customers.cust_id = Orders.cust_id
GROUP BY Customers.cust_id;
Combined Query
UNION
Use UNION
Give each SELECT statement, plus between UNION
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state IN ('IL', 'IN', 'MI')
UNION
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_name = 'Fun4All';
rule
Each query must contain the same column, expression, or aggregate functions, data types to be compatible
It contains duplicate rows
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state IN ('IL', 'IN', 'MI')
UNION ALL
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_name = 'Fun4All';