Command: Table Items and Wildcard %
Command: Table Items and Wildcard %
Command: Table Items and Wildcard %
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
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
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
adds up
COUNT(all)
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 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
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 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