List of SQL commands query syntax with examples
List of popular and important SQL commands with syntax and examples :
The SQL SELECT Statement : SELECT command is used to select data from a database table – Example –
- SELECT * FROM table_name;
- SELECT DISTINCT col1, col2 FROM table_name;
(SELECT DISTINCT command is used to get only distinct or different values)
WHERE clause : With the help of WHERE clause, records can be filtered on the based of some conditions. Example –
- SELECT col1, col2 FROM table_name WHERE Some_condition;
SQL commands with syntax and examples
AND, OR, and NOT operators can be used to combine WHERE clause –
AND and OR operators can be used when there are more then one condition.
The AND operator shows records only if all conditions used (or separated) by AND are true.
The OR operator shows records only if any condition separated by OR is TRUE.
The NOT operator shows records if condition used with NOT is Not TRUE.
See examples –
SELECT col1, col2 FROM table_name
WHERE condition1 AND condition2;
SELECT col1, col2 FROM table_name
WHERE condition1 OR condition2;
SELECT col1, col2 FROM table_name
WHERE NOT condition;
ORDER BY can be used to sort the result-records in ascending or descending order. By default it sorts records in ascending order but if you want result in descending order, use need to use DESC keyword. See examples –
SELECT col1, col2 FROM table_name
ORDER BY col1, col2 DESC;
INSERT INTO can be used to insert new records in a table. Example –
INSERT INTO table_name (col1, col2)
VALUES (value1, value2);
UPDATE can be used to update (or modify) the existing records in a table –
UPDATE table_name
SET col1 = value1, col2 = value2
WHERE condition;
DELETE can be used to delete table records.
DELETE FROM table_name
WHERE condition;
Important functions and keywords :
MIN() : This function can be used to get smallest value of the selected column.
MAX() : This function can be used to get largest value of the selected column.
Count() : This function can be used to get the number of rows.
SUM(): This function can be used to get the total sum of a column (numeric).
AVG(): This function can be used to get average value of a column(numeric).
Example :
SELECT MIN(column)
FROM table_name
WHERE condition;
SELECT COUNT(column)
FROM table_name
WHERE condition;
AS keyword can be used to rename column or table temporarily (an alias). Example –
SELECT column AS Alias_name FROM table_name;
This is very important & good information.. thanks for sharing this useful information..