USE sql_store;
SELECT *
FROM customers
WHERE state = ‘CA’
ORDER BY first_name
LIMIT 3;- SQL is not a case-sensitive language.
- In MySQL, every statement must be terminated with a semicolon.
- We use comments to add notes to our code. -- This is a comment and it won’t get executed.
- Using expressions
SELECT (points * 10 + 20) AS discount_factor FROM customers
- Order of operations:
- Parenthesis
- Multiplication / division
- Addition / subtraction
- Removing duplicates
SELECT DISTINCT state FROM customers
- We use the WHERE clause to filter data.
- Operators: >, <, >=, <=, =, <>, !=
- AND (both conditions must be True)
SELECT * FROM customers WHERE birthdate > ‘1990-01-01’ AND points > 1000
- OR (at least one condition must be True)
SELECT * FROM customers WHERE birthdate > ‘1990-01-01’ OR points > 1000
- NOT (to negate a condition)
SELECT * FROM customers WHERE NOT (birthdate > ‘1990-01-01’)
- Returns customers in any of these states: VA, NY, CA
SELECT * FROM customers WHERE state IN (‘VA’, ‘NY’, ‘CA’)
- Returns customers in the
pointsrange from 100 to 200SELECT * FROM customers WHERE points BETWEEN 100 AND 200
- Returns customers whose first name starts with b
SELECT * FROM customers WHERE first_name LIKE ‘b%’
- %: any number of characters
- _: exactly one character
- Returns customers whose first name starts with a
SELECT * FROM customers WHERE first_name REGEXP ‘^a’
- ^: beginning of a string
- $: end of a string
- |: logical OR
- [abc]: match any single characters
- [a-d]: any characters from a to d
- More Examples
- Returns customers whose first name ends with EY or ON
WHERE first_name REGEXP ‘ey$|on$’ - Returns customers whose first name starts with MY or contains SE
WHERE first_name REGEXP ‘^my|se’ - Returns customers whose first name contains B followed by R or U
WHERE first_name REGEXP ‘b[ru]’
- Returns customers whose first name ends with EY or ON
- Returns customers who don’t have a phone number
SELECT * FROM customers WHERE phone IS NULL
- Sort customers by state (in ascending order), and then by their first name (in descending order)
SELECT * FROM customers ORDER BY state, first_name DESC
- Return only 3 customers
SELECT * FROM customers LIMIT 3
- Skip 6 customers and return 3
SELECT * FROM customers LIMIT 6, 3
- Joins are used to combine columns from multiple table.
- We can use only join keyword as inner keyword is optional
SELECT name, o.product_id, quantity, o.unit_price FROM order_items o JOIN products p ON o.product_id = p.product_id
- In the case, if a column is present in more than one table then we have to use the table name to include that column
- In the below mentioned query,
productstable is not in the same table as that of theorder_itemstable, rather it is insql_inventorySELECT name, o.product_id, quantity, o.unit_price FROM order_items o JOIN products p ON o.product_id = p.product_id
-
SELECT * FROM employees e JOIN employees m ON e.reports_to = m.employee_id
-
SELECT order_id, order_date, c.first_name, c.last_name, os.name AS status FROM orders o JOIN customers c ON o.customer_id = c.customer_id JOIN order_statuses os ON o.status = os.order_status_id;
sql_storetable has two columns named,order_id&product_idtogether they served as composite primary keySELECT * FROM order_items oi JOIN order_item_notes oin ON oi.order_id = oin.order_id AND oi.product_id = oin.product_id
- Return all customers whether they have any orders or not
SELECT * FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id
- There are two OUTER join
- LEFT OUTER JOIN
- RIGHT OUTER JOIN
- Query with
LEFT OUTERjoin will contain all the records from left table. - Query with
RIGHT OUTERjoin will contain all the records from right table. OUTERkeyword is not necessary.
-
SELECT order_date, order_id, c.first_name, s.name AS shipper, os.name FROM orders o JOIN customers c ON o.customer_id = c.customer_id LEFT JOIN order_statuses os ON o.status = os.order_status_id LEFT JOIN shippers s ON o.shipper_id = s.shipper_id
- Avoid using different joins (left, right) in the same query.
-
SELECT e.employee_id, e.first_name, m.fitst_name AS manager FROM employees e LEFT JOIN employees m ON e.reports_to = m.employee_id
USINGclause is used to simplify the joining condition in-
SELECT order_date, order_id, c.first_name, s.name AS shipper, FROM orders o JOIN customers c USING(customer_id) LEFT JOIN shippers s USING(shipper_id)
-
SELECT * FROM shippers s CROSS JOIN products p
- Second syntax
SELECT * FROM shippers s,products p
- Union is use to combine rows from multiple tables.
SELECT customer_id, first_name, points, 'Bronze' AS type FROM customers WHERE points < 2000 UNION SELECT customer_id, first_name, points, 'Silver' AS type FROM customers WHERE points BETWEEN 2000 AND 3000 UNION SELECT customer_id, first_name, points, 'Gold' AS type FROM customers WHERE points > 3000 ORDER BY first_name
- Number of columns in both the querys for union must have same number of columns.
- Column names for the result is taken from the first query.
- Syntax-1 (order of the values must be same as that of the order of the columm)
INSERT INTO customers VALUES( 'Ram', 'kumar', NULL, 'address' 'state' DEFAULT )
- Syntax-2 (order of the values is not important)
INSERT INTO customers ( first_name, last_name, address, state, points ) VALUES( 'Ram', 'kumar', NULL, 'address' 'state' DEFAULT )
-
INSERT INTO products( name, quantity_in_stock, unit_price ) VALUES ('Table', 3, 50.0), ('Beads', 10, 20.0), ('Bead bag', 10, 50.0)
- This is used when there is a parent child relation between two tables. That is, when record in a table depends on other table.
INSERT INTO orders (customer_id, order_date, status) VALUES (1, '2020-01-05', 1); INSERT INTO order_items VALUES (LAST_INSERT_ID(), 1, 1, 2.95) (LAST_INSERT_ID(), 2, 1, 3.95)
-
CREATE TABLE invoices_archived AS SELECT invoice_id, number, c.name AS client_name, invoice_total, payment_total, invoice_date, due_date, payment_date FROM invoices i JOIN clients c USING(client_id) WHERE payment_date IS NOT NULL
-
UPDATE invoices SET payment_total = invoice_total * 0.5 payment_date = due_date -- due_daate is a column name WHERE invoice_id = 1
-
UPDATE orders SET comments = "Gold Customers" WHERE customer_id IN (SELECT customer_id FROM customers WHERE points > 3000)
- We can also use sub-queries with delete command
DELETE FROM invoices WHERE client_id = 3
- These functions only operate only on NON NULL value.
- Agregate: max(), min(), sum(), avg(), count()
SELECT 'First half of 2019' AS date_range, SUM(invoice_total) AS total_sales, SUM(payment_total) AS total_payments, SUM(invoice_total - payment_total) AS what_we_expect FROM invoices WHERE invoice_date BETWEEN '2019-01-01' AND '2019-06-30' UNION SELECT 'Second half of 2019' AS date_range, SUM(invoice_total) AS total_sales, SUM(payment_total) AS total_payments, SUM(invoice_total - payment_total) AS what_we_expect FROM invoices WHERE invoice_date BETWEEN '2019-07-01' AND '2019-12-31' UNION SELECT 'TOTAL' AS date_range, SUM(invoice_total) AS total_sales, SUM(payment_total) AS total_payments, SUM(invoice_total - payment_total) AS what_we_expect FROM invoices WHERE invoice_date BETWEEN '2019-01-01' AND '2019-12-31'
- Whenever we use aggregate functions then we have to use all the columns for GROUP BY.
SELECT date, name AS payment_method, SUM(amount) AS total_payments FROM payments p JOIN payment_methods pm ON p.payment_method = pm.payment_method_id GROUP BY date, name
- Having is used to filter data after grouping.
- Columns used in the having condition have to be the part of select clause.
SELECT c.customer_id, c.first_name, c.last_name, SUM(quantity * unit_price) AS totalSpent FROM customers c JOIN orders o USING(customer_id) JOIN order_items oi USING(order_id) WHERE state = 'VA' GROUP BY c.customer_id, c.first_name, c.last_name HAVING totalSpent > 100
- Roll up applyies only to the column that aggregate values
- When we GROUP BY multiple columns and use the ROLL UP operator, then result will be the values for each GROUP
SELECT name payment_method, -- no need of writting AS clause SUM(amount) total FROM payments p JOIN payment_methods pm ON p.payment_method = pm.payment_method_id GROUP BY name WITH ROLLUP