SQL Keywords
SQL keywords are reserved words used to perform various operations on the database. They are categorized based on their functionality, such as DDL, DML, TCL, DCL, and more.
Key Topics
1. DDL (Data Definition Language)
Keyword | Description |
---|---|
CREATE | Creates a new table, database, index, or view. |
ALTER | Modifies an existing database object. |
DROP | Deletes an entire table, view, or other object in the database. |
TRUNCATE | Removes all records from a table, including all spaces allocated for the records. |
RENAME | Renames a database object. |
COMMENT | Adds comments to the data dictionary. |
COMMENT ON | Provides comments on various database objects. |
REPLACE | Replaces an existing object with a new one. |
2. DML (Data Manipulation Language)
Keyword | Description |
---|---|
SELECT | Retrieves data from the database. |
INSERT | Inserts new data into a table. |
UPDATE | Modifies existing data in a table. |
DELETE | Removes data from a table. |
MERGE | Performs insert, update, or delete operations on a target table based on the results of a join with a source table. |
CALL | Executes a stored procedure. |
EXPLAIN | Displays the execution plan of a statement. |
3. TCL (Transaction Control Language)
Keyword | Description |
---|---|
COMMIT | Saves all changes made in the current transaction. |
ROLLBACK | Reverses all changes made in the current transaction. |
SAVEPOINT | Sets a point within a transaction to which you can later roll back. |
SET TRANSACTION | Specifies characteristics for the current transaction. |
BEGIN TRANSACTION | Starts a new transaction. |
END TRANSACTION | Ends the current transaction. |
4. DCL (Data Control Language)
Keyword | Description |
---|---|
GRANT | Gives users access privileges to the database. |
REVOKE | Removes access privileges given by the GRANT command. |
DENY | Denies a permission to a principal. |
5. DQL (Data Query Language)
Keyword | Description |
---|---|
SELECT | Retrieves data from the database. |
FROM | Specifies the table to retrieve data from. |
WHERE | Filters records based on a condition. |
GROUP BY | Groups rows that have the same values into summary rows. |
ORDER BY | Sorts the result-set in ascending or descending order. |
HAVING | Filters records that work on summarized GROUP BY results. |
6. DTL (Data Transaction Language)
Keyword | Description |
---|---|
BEGIN TRANSACTION | Starts a new transaction. |
COMMIT | Saves all changes made in the current transaction. |
ROLLBACK | Reverses all changes made in the current transaction. |
SAVEPOINT | Sets a point within a transaction to which you can later roll back. |
SET TRANSACTION | Specifies characteristics for the current transaction. |
7. Others
Keyword | Description |
---|---|
CASE | Creates conditional logic in queries. |
COALESCE | Returns the first non-null expression among its arguments. |
NULLIF | Returns NULL if the two expressions are equal. |
CAST | Converts an expression from one data type to another. |
CONVERT | Converts an expression from one data type to another with formatting options. |
EXISTS | Checks for the existence of any record in a subquery. |
IN | Specifies multiple possible values for a column. |
LIKE | Searches for a specified pattern in a column. |
BETWEEN | Filters the result set within a certain range. |
AND | Combines multiple conditions; all must be true. |
OR | Combines multiple conditions; at least one must be true. |
NOT | Negates a condition. |
AS | Assigns a temporary name to a table or a column for the duration of a query. |
NULL | Represents a missing or undefined value. |
TOP | Specifies the number of rows to return. |
DISTINCT | Removes duplicate records from the result set. |
UNION | Combines the result of two or more SELECT statements. |
INTERSECT | Returns the common records from two SELECT statements. |
MINUS | Returns the records from the first SELECT statement that are not in the second SELECT statement. |
OVER | Specifies the window for a window function. |
PARTITION BY | Divides the result set into partitions for window functions. |
ROW_NUMBER | Assigns a unique sequential integer to rows within a partition of a result set. |
RANK | Assigns a rank to each row within a partition of a result set. |
DENSE_RANK | Assigns a rank to each row within a partition of a result set without gaps. |
NTILE | Divides the result set into a specified number of approximately equal groups. |
LEAD | Accesses data from the next row in the result set. |
LAG | Accesses data from the previous row in the result set. |
FIRST_VALUE | Returns the first value in an ordered set of values. |
LAST_VALUE | Returns the last value in an ordered set of values. |
COUNT | Returns the number of input rows that match a specific condition of a query. |
SUM | Returns the sum of all or distinct values. |
AVG | Returns the average value of a numeric column. |
MIN | Returns the smallest value of the selected column. |
MAX | Returns the largest value of the selected column. |
GROUPING SETS | Allows multiple groupings in a single query. |
ROLLUP | Creates subtotals that roll up from the most detailed level to a grand total. |
CUBE | Creates subtotals for all possible combinations of a set of columns. |
Best Practices
- Use uppercase for SQL keywords for better readability.
- Properly format and indent your SQL code.
- Comment your code to explain complex queries.
- Organize keywords into appropriate categories to enhance understanding.
- Avoid using deprecated or obsolete keywords.
- Refer to official documentation for comprehensive keyword usage.
Key Takeaways
- SQL keywords are essential for performing various operations on databases.
- Understanding the categories of SQL keywords helps in writing effective queries.
- Proper usage of keywords ensures efficient and secure database management.
- Familiarity with a wide range of SQL keywords enhances your ability to handle complex database tasks.