SQL Commands
Command | Syntax | Explanation |
---|---|---|
SELECT | SELECT column1, column2, ... FROM table_name; | Retrieves data from one or more database tables. |
FROM | SELECT * FROM table_name; | Specifies the source table from which data is retrieved. |
WHERE | SELECT * FROM table_name WHERE condition; | Filters data based on a specified condition. |
DISTINCT | SELECT DISTINCT column1, column2, ... FROM table_name; | Retrieves unique values from specified columns. |
ORDER BY | `SELECT column1, column2, ... FROM table_name ORDER BY column1 ASC | DESC, column2 ASC |
GROUP BY | SELECT column1, column2, ... FROM table_name GROUP BY column1, column2, ...; | Groups rows with identical values in specified column(s). |
HAVING | SELECT column1, column2, ... FROM table_name GROUP BY column1, column2, ... HAVING condition; | Filters grouped data based on a condition. |
INSERT INTO | INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); | Inserts new rows into a table. |
UPDATE | UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; | Modifies existing records in a table. |
DELETE FROM | DELETE FROM table_name WHERE condition; | Deletes rows from a table based on a condition. |
COUNT | SELECT COUNT(column) FROM table_name; | Counts the number of rows in a result set or the number of occurrences of a specific value in a column. |
SUM | SELECT SUM(column) FROM table_name; | Calculates the sum of values in a numeric column. |
AVG | SELECT AVG(column) FROM table_name; | Calculates the average of values in a numeric column. |
MIN | SELECT MIN(column) FROM table_name; | Retrieves the minimum value from a column. |
MAX | SELECT MAX(column) FROM table_name; | Retrieves the maximum value from a column. |
BETWEEN | SELECT column1, column2, ... FROM table_name WHERE column BETWEEN value1 AND value2; | Filters data within a specified range. |
LIKE | SELECT column1, column2, ... FROM table_name WHERE column LIKE pattern; | Filters data based on a specified pattern. |
IN | SELECT column1, column2, ... FROM table_name WHERE column IN (value1, value2, ...); | Filters data based on a list of values. |
IS NULL | SELECT column1, column2, ... FROM table_name WHERE column IS NULL; | Filters data where a column is null. |
IS NOT NULL | SELECT column1, column2, ... FROM table_name WHERE column IS NOT NULL; | Filters data where a column is not null. |
JOIN | SELECT * FROM table1 JOIN table2 ON table1.column = table2.column; | Combines rows from two or more tables based on a related column. |
LEFT JOIN | SELECT * FROM table1 LEFT JOIN table2 ON table1.column = table2.column; | Retrieves all rows from the left table and matching rows from the right table. |
RIGHT JOIN | SELECT * FROM table1 RIGHT JOIN table2 ON table1.column = table2.column; | Retrieves all rows from the right table and matching rows from the left table. |
FULL OUTER JOIN | SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.column = table2.column; | Retrieves all rows when there is a match in either the left or right table. |
UNION | SELECT column1, column2, ... FROM table1 UNION SELECT column1, column2, ... FROM table2; | Combines the result sets of two or more SELECT statements into a single result set. |
UNION ALL | SELECT column1, column2, ... FROM table1 UNION ALL SELECT column1, column2, ... FROM table2; | Combines the result sets of two or more SELECT statements into a single result set, including duplicates. |
CREATE DATABASE | CREATE DATABASE database_name; | Creates a new database. |
DROP DATABASE | DROP DATABASE database_name; | Deletes an existing database. |
CREATE TABLE | CREATE TABLE table_name (column1 datatype, column2 datatype, ...); | Creates a new table with specified columns and data types. |
ALTER TABLE | ALTER TABLE table_name ADD column_name datatype; | Adds a new column to an existing table. |
DROP TABLE | DROP TABLE table_name; | Deletes an existing table. |
CREATE INDEX | CREATE INDEX index_name ON table_name (column1, column2, ...); | Creates an index on specified columns to improve query performance. |
DROP INDEX | DROP INDEX index_name; | Deletes an existing index. |
CREATE VIEW | CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition; | Creates a virtual table based on the result of a SELECT query. |
DROP VIEW | DROP VIEW view_name; | Deletes an existing view. |
GRANT | GRANT permissions ON object TO user; | Grants specific permissions on an object (table, view) to a user. |
REVOKE | REVOKE permissions ON object FROM user; | Revokes specific permissions on an object from a user. |
COMMIT | COMMIT; | Saves changes made during the current transaction. |
ROLLBACK | ROLLBACK; | Undoes changes made during the current transaction. |