SQL Commands Flashcards
display all records in a table
SELECT * FROM table_name;
display specified cols in a table
SELECT col_list FROM table_name;
display no extra copies, only unique values
SELECT DISTINCT col_list FROM table_name;
keyword to extract only those records w/specified criterion
WHERE
display data if from the country poland
SELECT col_list FROM table_name WHERE country = ‘Poland’;
display data if customer id is 2
SELECT col_list FROM table_name WHERE customerID = 2;
operators
, <=, >=, =, != or <>, BETWEEN, LIKE, IN
display data if from Berlin, Germany
SELECT col_list FROM table_name WHERE country = ‘Germany’ AND city = ‘Berlin’;
display data if from Boston, MA or Worcester,MA
SELECT col_list FROM table_name WHERE state = ‘MA” AND (city = ‘Boston’ OR city = ‘Worcester’);
display info in db and tables
SHOW
list db’s managed by server
SHOW DATABASES
display all tables in currently selected db
SHOW TABLES
display info about cols in given table
SHOW COLUMNS … SHOW COLUMNS FROM table_name;
display first 5 records from table
SELECT col_list FROM table_name LIMIT 5;
display 3, 4, 5th records from table
SELECT col_list FROM table_name LIMIT 2, 3;
typical display with fully qualified name
SELECT table_name.col FROM table_name;
typical display sorted a->z on col2
SELECT col_list FROM table_name ORDER BY col2;
typical display sorted z-a by country
SELECT col_list FROM table_name ORDER BY country DESC;
typical display sorted a-z by name then age
SELECT col_list FROM table_name ORDER BY name, age;
display data if age is 18-25
SELECT col_list FROM table_name WHERE age BETWEEN 18 AND 25;