USE sql_store;
SELECT *
FROM customers
ORDER BY first_name
SELECT customer_id,first_name,phone,address
FROM customers
WHERE phone LIKE '%9'
SELECT *
FROM customers
WHERE last_name REGEXP 'B[RU]'
SELECT *
FROM orders
WHERE shipped_date IS NULL
SELECT *,quantity * unit_price AS total_price
FROM order_items
WHERE order_id = 2
ORDER BY total_price DESC
SELECT *
FROM customers
ORDER BY points DESC
LIMIT 3
SELECT order_id,o.product_id,p.name,quantity,o.unit_price
FROM order_items o
JOIN products p
ON o.product_id = p.product_id
USE sql_inventory;
SELECT *
FROM sql_store.order_items oi
JOIN products p
ON oi.product_id = p.product_id
USE sql_hr;
SELECT
e.employee_id,
e.first_name,
m.first_name AS managerv0
FROM employees e
JOIN employees m
ON e.reports_to = m.employee_id
USE sql_store;
SELECT
o.order_id,
o.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
USE sql_invoicing;
SELECT
p.date,
p.invoice_id,
p.amount,
c.name,
pm.name
FROM payments p
JOIN clients c
ON p.client_id = c.client_id
JOIN payment_methods pm
ON p.payment_method = pm.payment_method_id
SELECT *
FROM order_items oi
JOIN order_item_notes oin
ON oi.order_id = oin.order_Id
AND oi.product_id = oin.product_id
SELECT *
FROM orders o
JOIN customers c
ON o.customer_id = c.customer_id
SELECT *
FROM orders o,customers c
WHERE o.customer_id = c.customer_id
SELECT
c.customer_id,
c.first_name,
o.order_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
ORDER BY c.customer_id
SELECT
p.product_id,
p.name,
oi.quantity
FROM products p
LEFT JOIN order_items oi
ON p.product_id = oi.product_id
SELECT
c.customer_id,
c.first_name,
o.order_id,
sh.name AS shipper
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
LEFT JOIN shippers sh
ON o.shipper_id = sh.shipper_id
ORDER BY c.customer_id
SELECT
o.order_date,
o.order_id,
c.first_name,
s.name AS shipper,
os.name AS status
FROM orders o
LEFT JOIN customers c
ON o.customer_id = c.customer_id
LEFT JOIN shippers s
ON o.shipper_id = s.shipper_id
LEFT JOIN order_statuses os
ON o.status = os.order_status_id
ORDER BY status
USE sql_hr;
SELECT
e.employee_id,
e.first_name,
m.first_name AS manager
FROM employees e
LEFT JOIN employees m
ON e.reports_to = m.employee_id
SELECT
o.order_id,
c.first_name,
sh.name AS shipper
FROM orders o
JOIN customers c
USING (customer_id)
LEFT JOIN shippers sh
USING (shipper_id)
SELECT *
FROM order_items oi
JOIN order_item_notes oin
USING (order_id,product_id)
USE sql_invoicing;
SELECT
p.date,
c.name AS client,
p.amount,
pm.name
FROM payments p
JOIN clients c
USING (client_id)
JOIN payment_methods pm
ON p.payment_method = pm.payment_method_id
SELECT
o.order_id,
c.first_name
FROM orders o
NATURAL JOIN customers c
SELECT
c.first_name AS customer,
p.name AS product
FROM customers c
CROSS JOIN products p
ORDER BY c.first_name
SELECT
c.first_name AS customer,
p.name AS product
FROM customers c,products p
ORDER BY c.first_name
SELECT
p.name,
s.name AS shipper
FROM products p,shippers s
ORDER BY p.name
SELECT
p.name,
s.name AS shipper
FROM products p
CROSS JOIN shippers s
ORDER BY p.name
SELECT
order_id,
order_date,
'Active' AS status
FROM orders
WHERE order_date >= '2019-01-01'
UNION
SELECT
order_id,
order_date,
'Archived' AS status
FROM orders
WHERE order_date < '2019-01-01';
SELECT first_name
FROM customers
UNION
SELECT name
FROM shippers
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
INSERT INTO customers (
first_name,
last_name,
birth_date,
address,
city,
state)
VALUES (
'John',
'Smith',
'1990-01-01',
'address',
'city',
'CA')
INSERT INTO shippers(name)
VALUES ('shipper1'),
('shipper2'),
('shipper3')
INSERT INTO products(name,quantity_in_stock,unit_price)
VALUES ('name1',1,1.1),
('name2',2,2.2),
('name3',3,3.3)
INSERT INTO orders(customer_id,order_date,status)
VALUES (1,'2019-01-02',1);
INSERT INTO order_items
VALUES
(LAST_INSERT_ID(),1,1,2.95),
(LAST_INSERT_ID(),2,1,3.95)
CREATE TABLE order_archived AS
SELECT * FROM orders3
INSERT INTO order_archived
SELECT *
FROM orders
WHERE order_date < '2019-01-01'
CREATE TABLE invoices_archived AS
SELECT
i.invoice_id,
i.number,
i.client_id,
c.name AS client,
i.invoice_total,
i.payment_total,
i.invoice_date,
i.due_date,
i.payment_date
FROM invoices i
JOIN clients C
USING (client_id)
WHERE payment_date IS NOT NULL
UPDATE invoices
SET payment_total = 10,payment_date = '2019-03-01'
WHERE invoice_id = 1
UPDATE invoices
SET payment_total = DEFAULT,payment_date = NULL
WHERE invoice_id = 1
UPDATE invoices
SET
payment_total = invoice_total * 0.5,
payment_date = due_date
WHERE invoice_id = 3
UPDATE invoices
SET
payment_total = invoice_total * 0.5,
payment_date = due_date
WHERE invoice_id IN (3,4)
UPDATE customers
SET
points = points + 50
WHERE birth_date < '1990-01-01'
UPDATE invoices
SET
payment_total = invoice_total * 0.5,
payment_date = due_date
WHERE invoice_id =
(SELECT client_id
FROM clients
WHERE name = 'Myworks')
UPDATE invoices
SET
payment_total = invoice_total * 0.5,
payment_date = due_date
WHERE invoice_id IN
(SELECT client_id
FROM clients
WHERE State IN ('CA','NY'))
UPDATE invoices
SET
payment_total = invoice_total * 0.5,
payment_date = due_date
WHERE payment_date IS NULL
UPDATE orders
SET comments = 'Gold customer'
WHERE customer_id IN
(SELECT customer_id
FROM customers
WHERE points > 3000)
DELETE FROM invoices
WHERE client_id = (
SELECT client_id
FROM clients
WHERE name = 'Myworks'
)
SELECT
MAX(invoice_total) AS highter,
MIN(invoice_total) AS lowest,
AVG(invoice_total) AS average,
SUM(invoice_total*1.1) AS total,
COUNT(DISTINCT client_id) AS total_records
FROM invoices
WHERE invoice_date > '2019-07-01'
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'
SELECT
state,
city,
SUM(invoice_total) AS total_sales
FROM invoices i
JOIN clients USING (client_id)
GROUP BY state,city
SELECT
date,
pm.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,payment_method
ORDER BY date
SELECT
client_id,
SUM(invoice_total) AS total_sales,
COUNT(*) AS number_of_invoices
FROM invoices
GROUP BY client_id
HAVING total_sales > 500 AND number_of_invoices > 5
USE sql_store;
SELECT
c.customer_id,
c.first_name,
c.last_name,
SUM(oi.quantity*oi.unit_price) AS total_sales
FROM customers c
JOIN orders o USING(customer_id)
JOIN order_items oi USING(order_id)
WHERE c.state = 'VA'
GROUP BY
c.customer_id,
c.first_name,
c.last_name
HAVING total_sales > 100
SELECT
state,
city,
SUM(invoice_total) AS total_sales
FROM invoices i
JOIN clients c USING (client_id)
GROUP BY state,city WITH ROLLUP
SELECT
pm.name AS payment_method,
SUM(amount) AS total
FROM payments p
JOIN payment_methods pm
ON p.payment_method = pm.payment_method_id
GROUP BY pm.name WITH ROLLUP
SELECT *
FROM products
WHERE unit_price > (
SELECT unit_price
FROM products
WHERE product_id = 3)
USE sql_hr;
SELECT *
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
)
SELECT *
FROM products
WHERE product_id NOT IN(
SELECT DISTINCT product_id
FROM order_items
)
USE invoicing;
SELECT *
FROM clients
WHERE client_id NOT IN(
SELECT DISTINCT client_id
FROM invoices
)
SELECT *
FROM clients
LEFT JOIN invoices USING (client_id)
WHERE invoice_id IS NULL
SELECT
customer_id,
first_name,
last_name
FROM customers
WHERE customer_id IN (
SELECT customer_id
FROM orders
WHERE order_id IN (
SELECT order_id
FROM order_items
WHERE product_id = 3
)
)
SELECT DISTINCT
c.customer_id,
c.first_name,
c.last_name
FROM customers c
LEFT JOIN orders o USING (customer_id)
LEFT JOIN order_items oi USING (order_id)
WHERE oi.product_id = 3
SELECT *
FROM invoices
WHERE invoice_total > (
SELECT MAX(invoice_total)
FROM invoices
WHERE client_id = 3
)
SELECT *
FROM invoices
WHERE invoice_total > ALL(
SELECT invoice_total
FROM invoices
WHERE client_id = 3
)
SELECT *
FROM clients
WHERE client_id = ANY (
SELECT client_id
FROM invoices
GROUP BY client_id
HAVING COUNT(*) >= 2
)
SELECT *
FROM clients
WHERE client_id IN (
SELECT client_id
FROM invoices
GROUP BY client_id
HAVING COUNT(*) >= 2
)
USE sql_hr;
SELECT *
FROM employees e
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE office_id = e.office_id
)
USE invoicing;
SELECT *
FROM invoices i
WHERE invoice_total > (
SELECT AVG(invoice_total)
FROM invoices
WHERE client_id = i.client_id
)
SELECT *
FROM clients
WHERE client_id IN (
SELECT DISTINCT client_id
FROM invoices
);
SELECT
DISTINCT client_id,
name,
address,
city,
state,
phone
FROM clients
JOIN invoices USING (client_id);
SELECT *
FROM clients c
WHERE EXISTS (
SELECT client_id
FROM invoices
WHERE client_id = c.client_id
)
USE sql_store;
SELECT *
FROM products p
WHERE NOT EXISTS(
SELECT product_id
FROM order_items
WHERE product_id = p.product_id
)
SELECT
invoice_id,
invoice_total,
(SELECT AVG(invoice_total)
FROM invoices) AS invoice_average,
invoice_total - (SELECT invoice_average) AS difference
FROM invoices
SELECT
client_id,
name,
(SELECT SUM(invoice_total)
FROM invoices
WHERE client_id = c.client_id) AS total_sales,
(SELECT AVG(invoice_total)
FROM invoices) AS average,
(SELECT total_sales - average) AS diffrence
FROM clients c
SELECT *
FROM (
SELECT
client_id,
name,
(SELECT SUM(invoice_total)
FROM invoices
WHERE client_id = c.client_id) AS total_sales,
(SELECT AVG(invoice_total)
FROM invoices) AS average,
(SELECT total_sales - average) AS diffrence
FROM clients c
) AS sales_summary
WHERE total_sales IS NOT NULL
SELECT ROUND(5.7345,2)
SELECT TRUNCATE(5.7345,3)
SELECT CEILING(5.2)
SELECT FLOOR(5.2)
SELECT ABS(-5.2)
SELECT RAND()
SELECT LENGTH('sky')
SELECT UPPER('sky')
SELECT LOWER('Sky')
SELECT LTRIM(' sky')
SELECT RTRIM('sky ')
SELECT TRIM(' sky ')
SELECT LEFT('Kindergarten',4)
SELECT RIGHT('Kindergarten',6)
SELECT SUBSTRING('Kindergarten',3,5)
SELECT LOCATE('n','Kindergarten')
SELECT REPLACE('Kindergarten','garten','garden')
SELECT CONCAT('first','last')
USE sql_store;
SELECT CONCAT(first_name,' ',last_name) AS full_name
FROM customers
SELECT NOW(),CURDATE(),CURTIME()
SELECT YEAR(NOW()),MONTH(NOW()),DAY(NOW()),HOUR(NOW()),MINUTE(NOW()),SECOND(NOW())
SELECT DAYNAME(NOW())
SELECT MONTHNAME(NOW())
SELECT EXTRACT(YEAR FROM NOW())
SELECT *
FROM orders
WHERE YEAR(order_date) = YEAR(NOW())-3
SELECT DATE_FORMAT(NOW(),'%M %d %Y')
SELECT TIME_FORMAT(NOW(),'%H:%i %p')
SELECT DATE_ADD(NOW(),INTERVAL 1 YEAR)
SELECT DATE_ADD(NOW(),INTERVAL -1 YEAR)
SELECT DATE_SUB(NOW(),INTERVAL 1 YEAR)
SELECT DATEDIFF('2019-01-05 09:00','2019-01-01 17:00')
SELECT DATEDIFF('2019-01-01 17:00','2019-01-05 09:00')
SELECT TIME_TO_SEC('09:00') - TIME_TO_SEC('09:02')
USE sql_store;
SELECT
order_id,
IFNULL(shipper_id,'Not assigned') AS shipper_1,
COALESCE(shipper_id,comments,'Not assigned') AS shipper_2
FROM orders
SELECT
CONCAT(first_name,' ',last_name) AS customer,
IFNULL(phone,'Unknown')
FROM customers
SELECT
order_id,
order_date,
IF(
YEAR(order_date) = YEAR(NOW()),
'Active',
'Archived') AS category
FROM orders
SELECT
product_id,
name,
count(*) AS orders,
IF(count(*) > 1, 'Many times','Once') AS frequency
FROM products
JOIN order_items USING (product_id)
GROUP BY product_id,name
SELECT
order_id,
order_date,
CASE
WHEN YEAR(order_date) = YEAR(NOW()) THEN 'Active'
WHEN YEAR(order_date) = YEAR(NOW()) - 1 THEN 'Last Year'
WHEN YEAR(order_date) < YEAR(NOW()) - 1 THEN 'Archived'
ELSE 'Future'
END AS category
FROM orders
SELECT
CONCAT(first_name,' ',last_name) AS coustomer,
points,
CASE
WHEN points > 3000 THEN 'Gold'
WHEN points >= 2000 THEN 'Silver'
ELSE 'Bronze'
END AS category
FROM customers
ORDER BY points DESC
USE invoicing;
CREATE VIEW sales_by_client AS
SELECT
c.client_id,
c.name,
SUM(invoice_total) AS total_sale
FROM clients c
JOIN invoices i USING (client_id)
GROUP BY client_id,name
CREATE VIEW client_balance AS
SELECT
client_id,
name,
SUM(invoice_total - payment_total) AS balance
FROM clients c
JOIN invoices i USING (client_id)
GROUP BY client_id,name
DROP VIEW sales_by_client
CREATE OR REPLACE VIEW sales_by_client AS
SELECT
c.client_id,
c.name,
SUM(invoice_total) AS total_sale
FROM clients c
JOIN invoices i USING (client_id)
GROUP BY client_id,name
create or replace view invoices_with_balance as
select
invoice_id,
number,
client_id,
invoice_total,
payment_total,
invoice_total - payment_total as balance,
invoice_date,
due_date,
payment_date
from invoices
where (invoice_total - payment_total) > 0
delete from invoices_with_balance
where invoice_id = 1
update invoices_with_balance
set due_date = date_add(due_date, interval 2 day)
where invoice_id = 2
create or replace view invoices_with_balance as
select
invoice_id,
number,
client_id,
invoice_total,
payment_total,
invoice_total - payment_total as balance,
invoice_date,
due_date,
payment_date
from invoices
where (invoice_total - payment_total) > 0
with check option
|