Commands Flashcards
SELECT * FROM tbl
Select all rows and columns from table tbl
SELECT c1,c2 FROM tbl
Select column c1,c2 and all rows from table tbl
SELECT c1,c2 FROM tbl WHERE conditions ORDER BY c1 ASC, c2 DESC
Select columns c1, c2 with where conditions and from able tbl. order results by column c1 in ascending order and c2 in descending order
SELECT DISTINCT c1,,c2 FROM tbl
Select unique rows by columns c1 and c2 from the table
SELECT c1, aggregate(expr) FROM tbl GROUP BY c1
Select column c1 and use aggregate function on expression expr, group columns by column c1
SELECT c1, aggregate(expr) AS c2 FROM tbl GROUP BY c1 HAVING c2 > v
Select column c1 and c2 as column alias of the result of aggregate function on expr. Filter group of records with c2 greater than value v
INSERT into tbl(c1,c2,…) VALUES(v1,v2…)
Insert data into table tbl
INSERT INTO tbl(c1,c2,…) SELECT c1,c2.. FROM tbl2 WHERE conditions
Insert data from tbl2 into tbl1
UPDAT tbl SET c1 = v1 c2 = v2 WHERE conditions
Update data in table tbl
DELETE FROM tbl WHERE conditions
Delete records from table tbl based on conditions
TRUNCATE TABLE tbl
Drop table tbl and recerate it, losing all data
CREATE TABLE tbl( c1 datatype(length) c2 datatype(length)… PRIMARY KEY(c1))
create a table with the primary key as c1
DROP TABLE tbl
Remove table tbl from database
ALTER TABLE tbl ADD COLUMN c1 datatype(length)
Add a column to the table
SELECT column_name FROM table_name
Gets you a column from your table
SELECT c1,c2 FROM table_1
Gets you the specific columns
SELECT DISTINCT column FROM table
Get the unique values from a column
SELECT COUNT(column) FROM table
Count all the rows in a column
SELECT COUNT( DISTINCT column) FROM TABLE
Gets you a number of distinct elements in a table
SELECT column FROM table WHERE condition
Lets you select a specific set of data
Example
SELECT column_1, column_2 FROM table ORDER BY column_1 ASC/DESC
Order the returned data by a specific column
SELECT company,name,sales FROM table ORDER BY company,sales
Orders the columns first by all unique companies, then by sales results