@@ -0,0 +1,24 @@ | |||
--A SELECT statement that retrieves all the data from the Invoices table | |||
SELECT * FROM invoices; | |||
--A SELECT statement that retrieves three columns from each row, sorted in descending sequence by invoice total | |||
SELECT invoice_number, invoice_date, invoice_total | |||
FROM invoices | |||
ORDER BY invoice_total DESC; | |||
--A SELECT statement that retrieves two columns and a calculated value for a specific invoice | |||
SELECT invoice_id, invoice_total, | |||
credit_total + payment_total AS total_credits | |||
FROM invoices | |||
WHERE invoice_id = 17; | |||
--A SELECT statement that retrieves two columns and a calculated value for a specific invoice | |||
SELECT invoice_number, invoice_date, invoice_total | |||
FROM invoices | |||
WHERE invoice_date BETWEEN '2018-06-01' AND '2018-06-30' | |||
ORDER BY invoice_date; | |||
--A SELECT statement that retrieves two columns and a calculated value for a specific invoice | |||
SELECT invoice_number, invoice_date, invoice_total | |||
FROM invoices | |||
WHERE invoice_total > 50000; |
@@ -0,0 +1,14 @@ | |||
SELECT vendor_name, | |||
CONCAT(vendor_city, ', ', vendor_state, ' ', vendor_zip_code) AS address | |||
FROM vendors | |||
ORDER BY vendor_name; | |||
SELECT vendor_name, | |||
CONCAT(vendor_city, ', ', vendor_state, ' ', vendor_zip_code) AS address | |||
FROM vendors | |||
ORDER BY vendor_name DESC; | |||
SELECT vendor_name, | |||
CONCAT(vendor_city, ', ', vendor_state, ' ', vendor_zip_code) AS address | |||
FROM vendors | |||
ORDER BY vendor_state, vendor_city, vendor_name; |
@@ -0,0 +1,17 @@ | |||
—An ORDER BY clause that uses an alias | |||
SELECT vendor_name, | |||
CONCAT(vendor_city, ', ', vendor_state, ' ', vendor_zip_code) AS address | |||
FROM vendors | |||
ORDER BY address, vendor_name; | |||
—An ORDER BY clause that uses an expression | |||
SELECT vendor_name, | |||
CONCAT(vendor_city, ', ', vendor_state, ' ', vendor_zip_code) AS address | |||
FROM vendors | |||
ORDER BY CONCAT(vendor_contact_last_name, vendor_contact_first_name); | |||
—An ORDER BY clause that uses column positions | |||
SELECT vendor_name, | |||
CONCAT(vendor_city, ', ', vendor_state, ' ', vendor_zip_code) AS address | |||
FROM vendors | |||
ORDER BY 2, 1; |
@@ -0,0 +1,14 @@ | |||
SELECT vendor_id, invoice_total | |||
FROM invoices | |||
ORDER BY invoice_total DESC | |||
LIMIT 5; | |||
SELECT invoice_id, vendor_id, invoice_total | |||
FROM invoices | |||
ORDER BY invoice_id | |||
LIMIT 2, 3; | |||
SELECT invoice_id, vendor_id, invoice_total | |||
FROM invoices | |||
ORDER BY invoice_id | |||
LIMIT 100, 1000; |
@@ -0,0 +1,10 @@ | |||
--A SELECT statement that renames the columns in the result se | |||
SELECT invoice_number AS "Invoice Number", invoice_date AS Date, | |||
invoice_total AS Total | |||
FROM invoices; | |||
--A SELECT statement that renames the columns in the result se | |||
SELECT invoice_number, invoice_date, invoice_total, | |||
invoice_total - payment_total - credit_total | |||
FROM invoices; | |||
@@ -0,0 +1,20 @@ | |||
--A SELECT statement that calculates the balance due | |||
SELECT invoice_total, payment_total, credit_total, | |||
invoice_total - payment_total - credit_total AS balance_due | |||
FROM invoices; | |||
--Use parentheses to control the sequence of operations | |||
SELECT invoice_id, | |||
invoice_id + 7 * 3 AS multiply_first, | |||
(invoice_id + 7) * 3 AS add_first | |||
FROM invoices | |||
ORDER BY invoice_id; | |||
--Use the DIV and modulo operators | |||
SELECT invoice_id, | |||
invoice_id / 3 AS decimal_quotient, | |||
invoice_id DIV 3 AS integer_quotient, | |||
invoice_id % 3 AS remainder | |||
FROM invoices | |||
ORDER BY invoice_id; |
@@ -0,0 +1,16 @@ | |||
--How to concatenate string data | |||
SELECT vendor_city, vendor_state, CONCAT(vendor_city, vendor_state) | |||
FROM vendors; | |||
--How to format string data using literal values | |||
SELECT vendor_name, | |||
CONCAT(vendor_city, ', ', vendor_state, ' ', vendor_zip_code) | |||
AS address | |||
FROM vendors; | |||
--How to include apostrophes in literal values | |||
SELECT CONCAT(vendor_name, '''s Address: ') AS Vendor, | |||
CONCAT(vendor_city, ', ', vendor_state, ' ', vendor_zip_code) | |||
AS Address | |||
FROM vendors; | |||
@@ -0,0 +1,21 @@ | |||
--A SELECT statement that uses the LEFT function | |||
SELECT vendor_contact_first_name, vendor_contact_last_name, | |||
CONCAT(LEFT(vendor_contact_first_name, 1), | |||
LEFT(vendor_contact_last_name, 1)) AS initials | |||
FROM vendors; | |||
--A SELECT statement that uses the DATE_FORMAT function | |||
SELECT invoice_date, | |||
DATE_FORMAT(invoice_date, '%m/%d/%y') AS 'MM/DD/YY', | |||
DATE_FORMAT(invoice_date, '%e-%b-%Y') AS 'DD-Mon-YYYY' | |||
FROM invoices | |||
ORDER BY invoice_date; | |||
--A SELECT statement that uses the ROUND function | |||
SELECT invoice_date, invoice_total, | |||
ROUND(invoice_total) AS nearest_dollar, | |||
ROUND(invoice_total, 1) AS nearest_dime | |||
FROM invoices | |||
ORDER BY invoice_date; | |||
@@ -0,0 +1,17 @@ | |||
--Four SELECT statements without FROM clauses | |||
--Example 1: Testing a calculation | |||
SELECT 1000 * (1 + .1) AS "10% More Than 1000"; | |||
--Example 2: Testing the CONCAT function | |||
SELECT "Ed" AS first_name, "Williams" AS last_name, | |||
CONCAT(LEFT("Ed", 1), LEFT("Williams", 1)) AS initials; | |||
--Example 3: Testing the DATE_FORMAT function | |||
SELECT DATE_FORMAT(CURRENT_DATE, '%m/%d/%y') AS 'MM/DD/YY', | |||
DATE_FORMAT(CURRENT_DATE, '%e-%b-%Y') AS 'DD-Mon-YYYY'; | |||
--Example 4: Testing the ROUND function | |||
SELECT 12345.6789 AS value, | |||
ROUND(12345.67) AS nearest_dollar, | |||
ROUND(12345.67, 1) AS nearest_dime; |
@@ -0,0 +1,9 @@ | |||
--A SELECT statement that returns all rows | |||
SELECT vendor_city, vendor_state | |||
FROM vendors | |||
ORDER BY vendor_city; | |||
--A SELECT statement that eliminates duplicate rows | |||
SELECT DISTINCT vendor_city, vendor_state | |||
FROM vendors | |||
ORDER BY vendor_city; |
@@ -0,0 +1,13 @@ | |||
—-A compound condition without parenthese | |||
SELECT invoice_number, invoice_date, invoice_total, | |||
(invoice_total - payment_total - credit_total) AS balance_due | |||
FROM invoices | |||
WHERE invoice_date > '2014-07-03' OR invoice_total > 500 | |||
AND invoice_total - payment_total - credit_total > 0; | |||
—The same compound condition with parentheses | |||
SELECT invoice_number, invoice_date, invoice_total, | |||
(invoice_total - payment_total - credit_total) AS balance_due | |||
FROM invoices | |||
WHERE (invoice_date > '2014-07-03' OR invoice_total > 500) | |||
AND invoice_total - payment_total - credit_total > 0; |
@@ -0,0 +1,16 @@ | |||
USE ex; | |||
SELECT * FROM null_sample; | |||
SELECT * FROM null_sample | |||
WHERE invoice_total = 0; | |||
SELECT * FROM null_sample | |||
WHERE invoice_total <> 0; | |||
SELECT * FROM null_sample | |||
WHERE invoice_total IS NULL; | |||
SELECT * | |||
FROM null_sample | |||
WHERE invoice_total IS NOT NULL; |