Saturday, September 6, 2014

Selection

SELECT * FROM my_table;

SELECT * FROM my_table WHERE first_name='Keith';

SELECT <column names> FROM my_table WHERE first_name='Keith';

AND/OR
SELECT location FROM my_table WHERE first_name='Keith' AND last_name='Tapp';

NOT EQUAL
<>

SELECT drink_name FROM drink_info WHERE drink_name >= 'L' AND drink_name < 'M';

LIKE

SELECT * FROM my_contact
WHERE
location = 'San Fran, CA'
OR
location = 'San Fran, CALIFORNIA'
OR
location = 'San Fransisco, CA'
OR
location = 'San Frans, CA'
OR
location = 'San Fransci, CA'
OR
location = 'SanDiago, CA'

SELECT * FROM my_contact WHERE location LIKE '%CA';
Finds all values in the location column that end with CA

% is stand-in for any number of characters
_ is stand-in for just one character

BETWEEN

SELECT drink_name FROM drink_info WHERE calories BETWEEN 30 AND 60;

order matters ... so BETWEEN 60 AND 30 would yield nothing

IN

SELECT data_name FROM black_book WHERE rating IN ('innovative', 'fabulous', 'delightful', 'pretty good');

NOT

SELECT data_name FROM black_book
WHERE
rating
NOT IN ('innovative', 'fabulous', 'delightful', 'pretty good');

SELECT * FROM easy_drinks
WHERE
NOT MAIN IS NULL;

When used with AND or OR, it goes right after the AND or OR

SELECT date_name from black_book
WHERE 
NOT date_name LIKE 'A%'
AND 
NOT date_name LIKE 'B%';

SELECT * FROM easy_drinks
WHERE 
NOT main = 'soda'
AND 
NOT main = 'iced tea';

SELECT drink_name FROM drink_info
WHERE 
NOT carbs BETWEEN 3 AND 5;

ORDER BY

You can tell SQL to select something and ORDER the data it returns BY another column from the table.
By default is ascending order.
To change order, use DESC after column name in ORDER BY clauses.

SELECT title, category
FROM movie_table
WHERE LIKE 'A%'
AND
CATEGORY = 'family'
ORDER BY title;

can order multiple columns in the same statement
SELECT title, category, purchased
FROM movie_table
ORDER BY category, purchased;

First ordered by category .. will get every movie in the store ordered by category
and then ordered by purchased ... after the category column has been ordered



SELECT title, purchased
FROM movie_table
ORDER BY title ASC, purchased DESC;

SUM

//Group together all first_name values
SELECT first_name, SUM(sales)
FROM cookie_sales
GROUP BY first_name
ORDER BY SUM(sales) DESC;

AVG

Average adds up all the values in a group and divides it by the number of entries in the group

SELECT first_name, AVG(sales)
FROM cookie_sales
GROUP BY first_name;

MAX/MIN

SELECT first_name, MAX(sales)
FROM cookie_sales
GROUP BY first_name;

COUNT

SELECT COUNT(sale_date)
FROM cookie_sales;

DISTINCT (keyword)

SELECT DISTINCT sale_date
FROM cookie_sales
ORDER BY sale_date;

SELECT COUNT (DISTINCT sale_date)
FROM cookie_sales

LIMIT

// Limit results to first 2

SELECT first_name, SUM(sales)
FROM cookie_sales
GROUP BY first_name
ORDER BY SUM(sales) DESC
LIMIT 2;

If you want to see top 20 to 30 songs

LIMIT 19, 10; // indexing starts from 0

i.e. LIMIT start index, number of them

No comments:

Post a Comment