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
CREATECreates a new table, database, index, or view.
ALTERModifies an existing database object.
DROPDeletes an entire table, view, or other object in the database.
TRUNCATERemoves all records from a table, including all spaces allocated for the records.
RENAMERenames a database object.
COMMENTAdds comments to the data dictionary.
COMMENT ONProvides comments on various database objects.
REPLACEReplaces an existing object with a new one.

2. DML (Data Manipulation Language)

Keyword Description
SELECTRetrieves data from the database.
INSERTInserts new data into a table.
UPDATEModifies existing data in a table.
DELETERemoves data from a table.
MERGEPerforms insert, update, or delete operations on a target table based on the results of a join with a source table.
CALLExecutes a stored procedure.
EXPLAINDisplays the execution plan of a statement.

3. TCL (Transaction Control Language)

Keyword Description
COMMITSaves all changes made in the current transaction.
ROLLBACKReverses all changes made in the current transaction.
SAVEPOINTSets a point within a transaction to which you can later roll back.
SET TRANSACTIONSpecifies characteristics for the current transaction.
BEGIN TRANSACTIONStarts a new transaction.
END TRANSACTIONEnds the current transaction.

4. DCL (Data Control Language)

Keyword Description
GRANTGives users access privileges to the database.
REVOKERemoves access privileges given by the GRANT command.
DENYDenies a permission to a principal.

5. DQL (Data Query Language)

Keyword Description
SELECTRetrieves data from the database.
FROMSpecifies the table to retrieve data from.
WHEREFilters records based on a condition.
GROUP BYGroups rows that have the same values into summary rows.
ORDER BYSorts the result-set in ascending or descending order.
HAVINGFilters records that work on summarized GROUP BY results.

6. DTL (Data Transaction Language)

Keyword Description
BEGIN TRANSACTIONStarts a new transaction.
COMMITSaves all changes made in the current transaction.
ROLLBACKReverses all changes made in the current transaction.
SAVEPOINTSets a point within a transaction to which you can later roll back.
SET TRANSACTIONSpecifies characteristics for the current transaction.

7. Others

Keyword Description
CASECreates conditional logic in queries.
COALESCEReturns the first non-null expression among its arguments.
NULLIFReturns NULL if the two expressions are equal.
CASTConverts an expression from one data type to another.
CONVERTConverts an expression from one data type to another with formatting options.
EXISTSChecks for the existence of any record in a subquery.
INSpecifies multiple possible values for a column.
LIKESearches for a specified pattern in a column.
BETWEENFilters the result set within a certain range.
ANDCombines multiple conditions; all must be true.
ORCombines multiple conditions; at least one must be true.
NOTNegates a condition.
ASAssigns a temporary name to a table or a column for the duration of a query.
NULLRepresents a missing or undefined value.
TOPSpecifies the number of rows to return.
DISTINCTRemoves duplicate records from the result set.
UNIONCombines the result of two or more SELECT statements.
INTERSECTReturns the common records from two SELECT statements.
MINUSReturns the records from the first SELECT statement that are not in the second SELECT statement.
OVERSpecifies the window for a window function.
PARTITION BYDivides the result set into partitions for window functions.
ROW_NUMBERAssigns a unique sequential integer to rows within a partition of a result set.
RANKAssigns a rank to each row within a partition of a result set.
DENSE_RANKAssigns a rank to each row within a partition of a result set without gaps.
NTILEDivides the result set into a specified number of approximately equal groups.
LEADAccesses data from the next row in the result set.
LAGAccesses data from the previous row in the result set.
FIRST_VALUEReturns the first value in an ordered set of values.
LAST_VALUEReturns the last value in an ordered set of values.
COUNTReturns the number of input rows that match a specific condition of a query.
SUMReturns the sum of all or distinct values.
AVGReturns the average value of a numeric column.
MINReturns the smallest value of the selected column.
MAXReturns the largest value of the selected column.
GROUPING SETSAllows multiple groupings in a single query.
ROLLUPCreates subtotals that roll up from the most detailed level to a grand total.
CUBECreates 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.