Basic SQL Statements
*) SQL create Database Statement
CREATE DATABASE databasename;
*) Drop Database Statement
DROP DATABASE database name;
*) SQL create Table Database Statement
CREATE TABLE table_name (
columnName1 dataType,
columnName2 dataType,
columnName3 dataType,
)
*) Drop Table Statement
DROP TABLE table_name;
*) SQL Alter Table Statement
ALTER TABLE table_name
ADD column_name datatype;
*) SQL Primary Key Statement
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
Age int,
PRIMARY KEY (ID)
);
*) SQL Auto Increment Statement
CREATE TABLE Persons (
ID int NOT NULL AUTO_INCREMENT,,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
Age int,
PRIMARY KEY (ID)
);
*) SQL Select Statement
SELECT column1, column2, ...
FROM table_name;
*) SQL Where Clause
SELECT column1, column2, ...
FROM table_nameWHERE condition;
*) SQL And, or, not Statement
And --> SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Or--> SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
Not--> SELECT column1, column2, ...
FROM table_nameWHERE NOT condition;
*) SQL Order by keyword
SELECT column1, column2, ...
FROM table_nameORDER BY column1, column2, ... ASC|DESC;
*) SQL Insert into Statement
INSERT INTO table_name
VALUES (value1, value2, value3, ...);*) SQL Null Values
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
*) SQL Update Statement
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
*) SQL Delete Statement
DELETE FROM table_name
WHERE condition;
*) SQL Min() & Max() Function
Min()
SELECT MIN(column_name)
FROM table_nameWHERE condition;
Max()
SELECT MAX(column_name)
FROM table_name
WHERE condition;
FROM table_name
WHERE condition;
*) SQL Sum() & Avg() & Count() Function
Sum()
SELECT SUM(column_name)
FROM table_nameWHERE condition;
Avg()
SELECT AVG(column_name)
FROM table_name
WHERE condition;
FROM table_name
WHERE condition;
Count()
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
FROM table_name
WHERE condition;
*) SQL Like Operator
- % - The percent sign represents zero, one, or multiple characters
- _ - The underscore represents a single character
SELECT column1, column2, ...
FROM table_nameWHERE column LIKE pattern;
*) SQL In Operator
SELECT column_name(s)
FROM table_nameWHERE column_name IN (value1, value2, ...);
*) SQL Between Operator
SELECT column_name(s)
FROM table_nameWHERE column_name BETWEEN value1 AND value2;
*) SQL Inner Join
SELECT column_name(s)
FROM table1INNER JOIN table2 ON table1.column_name = table2.column_name;
*) SQL Left Join
SELECT column_name(s)
FROM table1LEFT JOIN table2 ON table1.column_name = table2.column_name;
*) SQL Right Join
SELECT column_name(s)
FROM table1RIGHT JOIN table2 ON table1.column_name = table2.column_name;
*) SQL Full Join
SELECT column_name(s)
FROM table1FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
*) SQL Union Operator
SELECT column_name(s) FROM table1
UNIONSELECT column_name(s) FROM table2;
All ( To allow duplicate values )
SELECT column_name(s) FROM table1
UNION ALLSELECT column_name(s) FROM table2;
*) SQL Intersect Operator
SELECT column_name(s) FROM table1
Intersect
SELECT column_name(s) FROM table2;
Intersect
SELECT column_name(s) FROM table2;
All ( To allow duplicate values )
SELECT column_name(s) FROM table1
Intersect ALLSELECT column_name(s) FROM table2;
*) SQL Except Operator
SELECT column_name(s) FROM table1
Except
SELECT column_name(s) FROM table2;
Except
SELECT column_name(s) FROM table2;
All ( To allow duplicate values )
SELECT column_name(s) FROM table1
Except ALLSELECT column_name(s) FROM table2;
*) SQL Group By Statement
SELECT column_name(s)
FROM table_nameWHERE condition
GROUP BY column_name(s) ORDER BY column_name(s);
*) SQL Having Clause
SELECT column_name(s)
FROM table_nameWHERE condition
GROUP BY column_name(s) HAVING condition ORDER BY column_name(s);
0 Comments
If you have any doubts, Please let me know