Command
SHOW DATABASES
SHOW TABLES
SHOW COLUMNS FROM customers
SELECT city FROM customers
SELECT city FROM customers;
SELECT id FROM customers;
SELECT name, zip FROM customers
SELECT * FROM customers
SELECT DISTINCT state FROM customers
SELECT state FROM customers LIMIT 5
SELECT state FROM customers LIMIT 5, 10
Select state FROM customers ORDER BY state
Select state, adress FROM customers ORDER BY id
SELECT state, name FROM customers ORDER BY state, name
SELECT state, name FROM customers ORDER BY zip
SELECT state, name FROM customers ORDER BY zip DESC
SELECT state, name FROM customers ORDER BY zip DESC LIMIT 1
SELECT name, id FROM customers WHERE id=54
SELECT name, id FROM customers WHERE id != 54
SELECT name, id FROM customers WHERE id < 54
SELECT name, id FROM customers WHERE id BETWEEN 25 AND 30
SELECT name, id FROM customers WHERE name= 'BUCKY'
SELECT name, state, city FROM customers WHERE state= 'CA' AND city = 'Holywood'
SELECT name, state, city FROM customers WHERE state= 'CA' OR city = 'Holywood'
SELECT name, id, city FROM customers WHERE id=1 OR id=2 AND city='Raleigh'
SELECT name, id, city FROM customers WHERE id=1 OR (id=2 AND city='Raleigh')
SELECT name, id, city FROM customers WHERE (id=1 OR id=2) AND city='Raleigh'
SELECT name, state FROM customers WHERE state= 'CA' OR state = 'NC' OR state = 'NY'
SELECT name, state FROM customers WHERE state IN ('CA', 'NC', 'NY')
SELECT name, state FROM customers WHERE state IN ('CA', 'NC', 'NY') ORDER BY state
SELECT name, state FROM customers WHERE state NOT IN ('CA', 'NC', 'NY') ORDER BY state
Lets change the table please :p
Table=items and wildcard =%
What if we want have search like google at an site like amazon
SELECT name FROM items WHERE name LIKE 'new%'
SELECT name FROM items WHERE name LIKE '%new%'
SELECT name FROM customers WHERE name LIKE 'h%d'
Wildcard= '_'
This wild card is likehow many digits before or after etc
SELECT name FROM items WHERE name LIKE '_ boxes of frogs'
Regular expressions
SELECT name FROM items WHERE name REGEXP 'name'
.
SELECT name FROM items WHERE name REGEXP '.boxes'
| pipe
SELECT name FROM items WHERE name REGEXP 'gold|car'
[] Set
SELECT name FROM items WHERE name REGEXP '[12345] boxes of frog'
SELECT name FROM items WHERE name REGEXP '[1-5] boxes of frog'
SELECT name FROM items WHERE name REGEXP '[^12345] boxes of frog'
Creating Custom clolums
CONCAT ;concatination
SELECT CONCAT (city, ',', state) FROM customers
SELECT CONCAT (city, ',', state) AS new_address FROM customers
SELECT name, cost, cost-1 FROM items
SELECT name, cost, cost-1 AS sale_prcie FROM items
UPPER
SELECT name UPPER(name) FROM customers
SQRT
SELECT cost SQRT(cost) FROM items
AVG
SELECT AVG(cost) FROM items
SUM
SELECT SUM(bids) FROM items
COUNT
SELECT COUNT(name) FROM items WHERE seller_id=6
Mix
SELECT COUNT(*) AS item_count,
MAX(cost) AS max,
AVG(cost) AS avg
FROM items WHERE seller_id=12
GROUP BY
SELECT seller_id, COUNT(*) FROM items GROUP BY seller_id,
HAVING
SELECT seller_id, COUNT(*) AS item_count FROM items GROUP BY seller_id HAVING COUNT(*) >=3
SELECT seller_id, COUNT(*) FROM items GROUP BY seller_id HAVING COUNT(*) >=3 ORDER BY item_count DESC
Query with in a Query
SELECT AVG(cost) FROM items=463 lets suppose
SELECT name,cost FROM items WHERE cost>(SELECT AVG(cost) FROM items) ORDER BY cost DESC
Joining tables together
discription
with in DB
Will show all columns from table of customers
show just city column entries from table customers
Use semi-colin for multiple queries
Shows both colums i.e name and zip
Shows all columns in customers table
If you dont want to repeat the data in column
It will show first 5 (0-4) entries of column state from table customers
It will show (6th-5+10th) entries of column state from table customers
Will order alphabetically the column state
Will order state and adress columns according to id column numerically
Will order according to state whole thing then with in state will order by name
order according to zip Assending trend
order according to zip Dessending trend
Just 1 highest zip entry of both name and state
filter out name of id=54
filter out name of id not equal to 54
filter out name of id is less than 54
Clauses are must
we can use IN statement except bunch or OR statements
Same out put as mentioned woth multiple ORs
show all rest of states means except NC NY CA
This % sign is going to be treated as anything like everything starting with new like new gym newspaper etc
This % sign is going to be treated as anything like everything with new whether before or after anywhere like new gym , imac new co
If we put % in middle then it will check both ends of word like in SS
Check SS for results
To search more complex patterns than just using _%
it will show all results containing new in them whether its at start middle or at end or anywhere
everthing that contain boxes after. means space and whatever
Same as OR a|b a or b
Check SS for results
same like multiple ORs
show all results that contain 1,2,3,4,5 boxes of frog
show all results that contain 1,2,3,4,5 boxes of frog
show all results that contain except 1,2,3,4,5 boxes of frog negate*
is used to joint different colums in one line
Check SS for results
if you want to name this new column
it will show three columns i.e name cost and cost-1
if you wanna name it
To convert into uppercase alphabets
cost and sqrt(cost)
It takes average
adds up
going to count number of rows
COUNT(all)
It groups all queries
SELECT seller_id, COUNT(*) FROM items WHERE seller_id=1
SELECT seller_id, COUNT(*) FROM items WHERE seller_id=2
SELECT seller_id, COUNT(*) FROM items WHERE seller_id=3
is same as where but in GROUP BY we use HAVING
Calculates Average
Will show all columns whose cost is greater than average in decending order
where like new gym , imac new computer, newspaper etc
SHOW COLUMNS FROM customers
SELECT city FROM costumers
SELECT namShows both colums i.e name and zip
SELECT state FROM customers
As NY is repeatin and you want distinct like all states in table
SELECT DISTINCT state FROM customers
SELECT stat It will show (6th-5+10th) entries of column state from table customers
SELECT sta Will order according to state whole thing then with in state will order by name
SELECT namfilter out name of id not equal to
SELECT namfilter out name of id=54
SELECT name, id, city FROM customers WHERE id=1 OR id=2 AND city='Raleigh'
It read as
SELECT name, id, city FROM customers WHERE id=1 OR (id=2 AND city='Raleigh')
it must read as
SELECT name, id, city FROM customers WHERE (id=1 OR id=2) AND city='Raleigh'
SELECT namThis % sign is going to be treated as anything like new gym newspaper etc
SELECT namIf we put % in middle then it will check both ends of word like in SS
any thing in column that start with 'h' and end at 'd'
SELECT namCheck SS for results
only one _ than only one character like 3 or 7 not 48
but if we use % it would include everything before
SELECT name FROM items WHERE name LIKE '% boxes of frogs'
it will show everthing ening at boxes of frogs
REGULAR EXPRESSIONS
SELECT namit will show all results containing new in them whether its at start middle or at end or anywhere
SELECT name FROM items WHERE name REGEXP 'gold|car'
SELECT namshow all results that contain 1,2,3,4,5 boxes of frog
SELECT CONCAT (city, ',', state) FROM customers
SELECT name
it will show three columns i.e name cost and cost-1
SELECT name
if you wanna name it
SELECT seller_id, COUNT(*) FROM items GROUP BY seller_id,
filter out name of id not equal to 54
dlc 9د