CREATE VIEW view_name AS SELECT columns FROM tables [WHERE conditions];
ALTER VIEW view_name AS SELECT columns FROM table WHERE conditions;
DROP VIEW IF EXISTS view_name;
CREATE VIEW Trainer AS SELECT c.course_name, c.trainer, t.email
FROM courses c, contact t WHERE c.id = t.id;
-- statement
& # statement
SELECT 55 + 11;
SELECT now();
SELECT curdate();
SELECT ucase();
DESC tableName
=> Describes the tableUPPER(colName)
=> Turns into uppercaseLTRIM(colName)
=> Trims white spaces from leftRTRIM(colName)
=> Trims white spaces from rightLENGTH(colName)
=> Returns length of the stringINSTR(colName, 'C')
=> Insert a character in the string at given positionsubstring(colName, N, M)
=> Returns substringREPLACE(colName, 'C1', 'C2')
=> Replaces from stringCONCAT(colName1, " ", colName2)
=> Joins stringsSELECT * FROM tableName LIMIT 5
=> Limits result by n numbersLIMIT n1 n2
=> Limits result by n2 numbers after an offset (skip) of n1 numbersCREATE DATABASE IF NOT EXISTS dbName;
=> Creates DatabaseUSE dbName;
=> Selects database, Switch databaseDROP DATABASE IF EXISTS dbName;
=> Dropping databaseSHOW DATABASES;
=> List all the DBs in the serverSHOW TABLES;
=> List tables in the selected DB CREATE TABLE tableName1
(
colName1 dataType(n),
colName2 dataType(n),
PRIMARY KEY(colName)
)
CREATE TABLE tableName1 AS
SELECT * FROM tableName2
CREATE TABLE ORDER (
id INT PRIMARY KEY,
delivery_date DATE,
order_placed_date DATE,
cust_id INT,
FOREIGN KEY (cust_id) REFERENCES customer(id)
);
CREATE TABLE customer (email VARCHAR(1024) UNIQUE);
CREATE TABLE customer (CONSTRAINT age_check CHECK (age > 12));
CREATE TABLE account (saving-rate DOUBLE NOT NULL DEFAULT 4.25);
ALTER TABLE tableName ADD newColName datatype ADD newColName2 datatype;
ALTER TABLE customer ADD age INT NOT NULL;
ALTER TABLE table-name MODIFY col-name col-datatype;
ALTER TABLE customer MODIFY name CHAR(1024);
ALTER TABLE tableName CHANGE COLUMN oldColName newColName new-col-datatype;
ALTER TABLE customer CHANGE COLUMN name customer-name VARCHAR(1024);
ALTER TABLE tableName DROP COLUMN colName;
ALTER TABLE customer DROP COLUMN middle-name;
ALTER TABLE tableName RENAME TO newTableName;
ALTER TABLE customer RENAME TO customer-details;
SELECT colName1, colName2 FROM tableName;
SELECT *
=> Select all ColumnsSELECT colName1, colName2
=> Select given Columns in given OrderSELECT colName1 + n
=> Perform Arithematic operation on a Column, Use brackets when taking care of orderFROM tableName
=> Select a Table after selecting a DatabaseFROM databaseName.tableName
=> Select a Table and Database togetherFROM tableName aliasName
=> Give alias name to a tableSELECT colName AS alias_name FROM tableName;
SELECT colName1, colName2 FROM tableName AS alias_name;
SELECT 'string' AS aliasName
=> Creates a Column of name alias with value equal to given stringSELECT * FROM customer WHERE age > 18;
WHERE condition
=> Select Columns if it satisfies the condition, Conditions applied on ColumnsWHERE condition1 AND condition2
=> Apply multiple conditions using Logical operators, Use brackets when taking care of orderWHERE colName IN ('value1', 'value2')
=> Selects rows where values of Column are equal to given values, IN is used for multiple valuesWHERE colName BETWEEN value1 AND value2
=> Values are inclusiveWHERE colName LIKE 'value%'
=> Percent represent any number of characterWHERE colName LIKE 'value_'
=> _ represent single characterWHERE colName REGEXP 'value'
=> Similar to LIKE operatorWHERE colName REGEXP '^value'
=> ^ represent beginning of a stringWHERE colName REGEXP 'value$'
=> $ represent end of a stringWHERE colName REGEXP 'value1|value2'
=> Search for multiple patternWHERE colName REGEXP '[value1value2]value'
=> value1 or value2 should be present before valueWHERE colName REGEXP '[value1-value2]'
=> search for range of valuesSELECT * FROM customer WHERE age BETWEEN 0 AND 100;
OR
conditionsSELECT * FROM officers WHERE officer_name IN ("Lakshay", "Pratap", "Deepika");
WHERE cond1 AND cond2
WHERE cond1 OR cond2
WHERE colName NOT IN (1,2,3,4);
SELECT * FROM customer WHERE prime_status is NULL;
SELECT * FROM customer WHERE name LIKE "%p_";
ORDER BY colName DESC;
SELECT * FROM customer ORDER BY name DESC;
ORDER BY colName
=> Sort the Data by given Column in ascending order, Column can even be an Alias name/ExpressionORDER BY colName1, colName2
=> If values matches in column1 then sort it by column2SELECT DISTINCT(colName) FROM tableName;
Select colName from table GROUP BY colName;
SELECT colName1, colName2, colName3 FROM tableName WHERE condition GROUP BY colName1, colName2, colName3
Select COUNT(cust_id), country from customer GROUP BY country HAVING COUNT(cust_id) > 50;
INSERT INTO tableName
# Give values for each Column based on its attributes defined
VALUES (value1, value2)
INSERT INTO tableName (colName1, colName2)
VALUES (value1, value2)
INSERT INTO tableName
VALUES (DEFAULT, value1, value2, NULL)
INSERT INTO tableName
VALUES (value1, value2),
(value3, value4)
INSERT INTO tableName
VALUES (LAST_INSERT_ID(), value1, value2)
INSERT INTO tableName1
SELECT *
FROM tableName2
WHERE condition
UPDATE tableName SET col1 = 1, col2 = ‘abc’ WHERE id = 1;
UPDATE student SET standard = standard + 1;
SET SQL_SAFE_UPDATES = 0;
=> Turn off safe mode UPDATE tableName
# Change the value of Column with the value given
SET colName = value
# Where clause is optional
WHERE condition
UPDATE tableName
SET colName = value
# Use IN instead of equal sign when dealing with multiple values
WHERE colName = (SELECT * FROM tableName WHERE condition)
DELETE FROM tableName WHERE id = 1;
DELETE FROM tableName;
=> All rows will be deleted CREATE TABLE ORDER (
order_id int PRIMARY KEY,
delivery_date DATE,
cust_id INT,
FOREIGN KEY(cust_id) REFERENCES customer(id) ON DELETE CASCADE
);
CREATE TABLE ORDER (
order_id int PRIMARY KEY,
delivery_date DATE,
cust_id INT,
FOREIGN KEY(cust_id) REFERENCES customer(id) ON DELETE SET NULL
);
REPLACE INTO student (id, class) VALUES(4, 3);
REPLACE INTO table SET col1 = val1, col2 = val2;
INNER JOIN
can also be written as just JOIN
SELECT column-list FROM table1 INNER JOIN table2 ON condition1 INNER JOIN table3 ON condition2;
FROM tableName1
JOIN tableName2 ON condition1
JOIN tableName3 ON condition2
FROM tableName1
JOIN tableName2 USING (colName1, colName2)
FROM tableName1
NATURAL JOIN tableName2
FROM database.tableName1
INNER JOIN tableName2 ON condition1 AND condition2
SELECT * FROM table1, table2 WHERE condition;
SELECT artist_name, album_name, year_recorded FROM artist, album WHERE artist.id = album.artist_id;
LEFT JOIN
can also be written as LEFT OUTER JOIN
SELECT columns FROM table LEFT JOIN table2 ON Join_Condition;
RIGHT JOIN
can also be written as RIGHT OUTER JOIN
SELECT columns FROM table RIGHT JOIN table2 ON join_cond;
SELECT columns FROM table1 as t1 LEFT JOIN table2 as t2 ON t1.id = t2.id
UNION
SELECT columns FROM table1 as t1 RIGHT JOIN table2 as t2 ON t1.id = t2.id;
statement1 UNION statement2
=> Creates union of both values, Number of query returned should be equalSELECT column-lists FROM table1 CROSS JOIN table2;
SELECT columns FROM table as t1 INNER JOIN table as t2 ON t1.id = t2.id;
SELECT * FROM table1 UNION SELECT * FROM table2;
UNION ALL
=> Can also be used, this will duplicate values as well while UNION gives unique valuesSELECT DISTINCT column-list FROM table1 INNER JOIN table2 USING(join_cond);
SELECT DISTINCT * FROM table1 INNER JOIN table2 ON USING(id);
SELECT columnName FROM table1 LEFT JOIN table2 USING(columnName) WHERE table2.columnName IS NULL;
SELECT id FROM table1 LEFT JOIN table2 USING(id) WHERE table2.id IS NULL;
SELECT columnNames FROM tableName WHERE columnName OPERATOR
(SELECT columnNames FROM tableName [WHERE]);
SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1);
SELECT MAX(rating) FROM (SELECT \* FROM movie WHERE country = ‘India’) as temp;
SELECT (SELECT columnNames FROM T_name WHERE condition), columnList FROM T2_name WHERE condition;
SELECT columnLists FROM (SELECT columnLists FROM tableName WHERE [condition]) as new_tableName;
SELECT * FROM worker w1 WHERE 4 = (SELECT count(distinct(w2.salary)) FROM worker w2 WHERE w2.salary >= w1.salary);