Introduction of SQL Keywords, Or List of SQL Reserved Words
Summary: You will learn in this article, Introduction of SQL Keywords, Or List of SQL Reserved Words. These SQL keywords reference contains the reserved words in SQL. basically, these SQL Keywords are part of the SQL Server T-SQL grammar. I hope you will enjoy SQL Keywords article.
Introduction To SQL Keywords
The SQL keywords are the reserved words it is used for the purpose of performing various operations in the database. There are several keywords in SQL and as SQL is case insensitive, it doesn’t matter if we use for example SELECT or select.
- SQL Server basically, uses reserved keywords for database operations.
- Therefore, these keywords in SQL are part of the SQL Server T-SQL grammar.
- SQL Server has claimed current & future SQL reserved words.
- Keywords are often used as identifiers by putting them between [ ] (square brackets).
List of SQL Keywords
1. CREATE | 2. PRIMARY KEY | 3. INSERT | 4. SELECT |
5. FROM | 6. ALTER | 7. ADD | 8. DISTINCT |
9. UPDATE | 10. SET | 11. DELETE | 12. TRUNCATE |
13. AS | 14. ORDER BY | 15. ASC | 16. DESC |
17. BETWEEN | 18. WHERE | 19. AND | 20. OR |
21. NOT | 22. LIMIT | 23. IS NULL | 24. DROP |
25. DROP COLUMN | 26. DROP DATABASE | 27. DROP TABLE | 28. GROUP BY |
29. HAVING | 30. IN | 31. JOIN | 32. UNION |
33. UNION ALL | 34. EXISTS | 35. LIKE | 36. CASE |
Explanation of SQL Keywords
Furthermore, these SQL keywords can be used as explained in the given below examples for several operations.
1). CREATE
The CREATE Keyword in SQL is used for the purpose of creating a database, table, views, and index. We can create the table CUSTOMERS as below.
1 2 3 4 5 |
CREATE TABLE CUSTOMERS ( CUSTOMERS_ID INT PRIMARY KEY, CUSTOMERS_NAME VARCHAR(40), CITY VARCHAR(20) ); |
2). PRIMARY KEY
This keyword uniquely identifies every one of the records. A SQL Server Database can be created with the usage of CREATE DATABASE statement as given below:
1 |
CREATE DATABASE DATABASE_NAME; |
A View in SQL can be created by the utilization of CREATE VIEW as given below:
1 2 3 |
CREATE VIEW VIEW_NAME AS SELECT COLUMN1, COLUMN2, COLUMN3, COLUMN4... FROM TABLE_NAME WHERE [CONDITION]; |
3). INSERT
The SQL INSERT Keyword is utilized for the purpose of inserting the rows of data into a table. Furthermore, we can insert the below rows to the already created CUSTOMERS table by using the given below queries.
1 2 3 4 |
INSERT INTO CUSTOMERS VALUES (111,'Shyam','Noida'); INSERT INTO CUSTOMERS VALUES(121,'Lalit','Ghaziabad'); INSERT INTO CUSTOMERS VALUES(117,'Saurabh','Kanpur'); INSERT INTO CUSTOMERS VALUES(115,'Jitendra','Jhasi'); |
Therefore, as per the above-given statements will insert the rows into the table “CUSTOMERS”
. after that, we can see the result by using a simple SQL SELECT statement as given below.
1 |
SELECT * FROM CUSTOMERS; |
4). SELECT
This SELECT
keyword is used to select the data from the database or table. The ‘*’
is utilized in the select statement to select all the columns in a table.
1 |
SELECT CUSTOMERS_NAME FROM CUSTOMERS; |
The result of the given above query will display the column CUSTOMERS_NAME
from the CUSTOMERS
table as below.
5). FROM
The keyword in SQL is used to point to the table from which the data is selected or deleted.
6). ALTER
The Keyword ALTER
in SQL is used to modify the columns in tables. Furthermore, the ALTER COLUMN statement modifies the data type of a column & the ALTER TABLE modifies the columns by adding or deleting them. We can modify the columns of the CUSTOMERS
table as given below by adding a new column “PHONE”
.
1 2 |
ALTER TABLE CUSTOMERS ADD PHONE INT; SELECT * FROM CUSTOMERS; |
This query statement above will add the new column “PHONE”
with values for all the rows as null. Furthermore, the given above statement uses another SQL keyword ‘ADD’
.
7). ADD
This keyword is used to add a column to the existing or present table.
8). DISTINCT
The keyword SQL DISTINCT is used to select distinct values. Furthermore, we can use SQL SELECT DISTINCT to select only the distinct values from a table.
Therefore, let us add a duplicate value for the state Ghaziabad as below:
1 |
INSERT INTO CUSTOMERS VALUES(161, 'Aditya', 'Ghaziabad','NULL'); |
Therefore, the CUSTOMERS
table has now the below rows
1 |
SELECT * FROM CUSTOMERS; |
Now we will see the distinct values for the column CITY
by utilizing the below query:
1 |
SELECT DISTINCT(CITY) FROM CUSTOMERS; |
9). UPDATE
This keyword in SQL can be used in an SQL statement to update the existing rows in a table.
1 2 |
UPDATE CUSTOMERS SET CITY ='Lucknow' WHERE CUSTOMERS_ID= 117; SELECT * FROM CUSTOMERS; |
The CUSTOMERS_ID with value 117 is updated with a new city Lucknow
.
10). SET
This Keyword in SQL is used basically to specify the column or values to be updated.
11). DELETE
SQL Delete Keyword is used for the purpose of deleting the existing rows from a table.
1 |
DELETE FROM CUSTOMERS WHERE CUSTOMERS_NAME='Lalit'; |
The given above query will display the below as the row with CUSTOMERS_NAME
as Lalit is deleted from the result set.
Furthermore, while using the SQL DELETE keyword, if we don’t use the WHERE clause, then all the records will be deleted from the table.
1 |
DELETE FROM CUSTOMERS; |
The given above query will delete all the records of the CUSTOMERS table.
12). TRUNCATE
This keyword is used to delete the data in a table, however, it doesn’t delete the structure of the table.
1 |
TRUNCATE TABLE CUSTOMERS; |
The given above query only deletes the data but the structure of the table remains. therefore there’s no need to re-create the table.
13). AS
The Keyword AS in SQL is used as an alias to rename the column or table.
1 |
SELECT CUSTOMERS_ID AS CUST_ID, CUSTOMERS_NAME AS NAME FROM CUSTOMERS; |
The given above statement will create the alias for the columns CUST_ID and NAME as below:
14). ORDER BY
The ORDER BY keyword is used to sort the result in descending or ascending order. Furthermore, this keyword sorts the result by default in ascending order.
15). ASC
It’s used for sorting the data returned by the SQL query in ascending order.
1 |
SELECT * FROM CUSTOMERS ORDER BY CUSTOMERS_NAME ASC; |
The given above query will select all the columns from the CUSTOMERS table and it sorts the data by the NAME column in ascending order.
16). DESC
This SQL DESC keyword is to sort the result set in descending order.
1 |
SELECT * FROM CUSTOMERS ORDER BY CUSTOMERS_NAME DESC; |
The given above SQL query will sort all the selected fields of the table with the descending order of CUSTOMERS_NAME
.
17). BETWEEN
This SQL BETWEEN keyword is utilized to select values within a given range. Furthermore, the below query uses the BETWEEN keyword to select the CUSTOMERS_ID and CUSTOMERS_NAME within a given range of values for the CUSTOMERS_ID.
1 |
SELECT CUSTOMERS_ID, CUSTOMERS_NAME FROM CUSTOMERS WHERE CUSTOMERS_ID BETWEEN 100 AND 160; |
The given above SQL query will give the below result
18). WHERE
This SQL WHERE keyword is used to filter the result set so that only the values satisfying the condition are included
1 |
SELECT * FROM CUSTOMERS WHERE CITY ='Lucknow'; |
The given above SQL query selects all the values from the CUSTOMERS table for which the CITY is Lucknow.
19). AND
This SQL AND keyword are used along with the WHERE clause to select the rows for which both the conditions are true.
1 |
SELECT * FROM CUSTOMERS WHERE CITY ='Lucknow' AND CUSTOMERS_ID= 117; |
The given above SQL query will give the result as below
But suppose that if one of the conditions isn’t satisfied, then the query will not return any result as CITY in the below query.
1 |
SELECT * FROM CUSTOMERS WHERE CITY ='Lucknow' AND CUSTOMERS_ID= 114; |
20). OR
This SQL OR Keyword is used with the SQL WHERE clause to include the rows in the result set in case of either condition is true.
The given below SQL statement will select the fields from the CUSTOMERS table if the CITY is Lucknow or Noida.
1 |
SELECT * FROM CUSTOMERS WHERE CITY='Lucknow' OR CITY='Noida'; |
Furthermore, in the case of the SQL OR keyword, we can see from the above result that in the case any of the given conditions are true, then that gets included in the result set.
21). NOT
The SQL NOT keyword is used with an SQLWHERE clause to include the rows in the result set where a condition is not true. We can use the SQL NOT keyword in the given below SQL query to not include the rows from the CITY Lucknow as below.
1 |
SELECT * FROM CUSTOMERS WHERE NOT CITY = 'Lucknow'; |
The SQL query will return the rows with the other cities excluding Lucknow in the result set as below:
22). LIMIT
This SQL LIMIT keyword retrieves the records from the table in order to limit them based on the limit value.
1 |
SELECT * FROM CUSTOMERS LIMIT 2; |
The given above SQL query will select the records from the table CUSTOMERS but it will display only the three(3) rows of data from the table.
23). IS NULL
The SQL keyword IS NULL is used for the pur[pose of checking for NULL values.
The given below SQL query will show all the records for which the PHONE column has NULL values.
1 |
SELECT * FROM CUSTOMERS WHERE 'PHONE' IS NULL; |
IS NOT NULL
This is utilized for the purpose of searching the NOT NULL values.
1 |
SELECT * FROM CUSTOMERS WHERE CITY IS NOT NULL; |
As column CITY
has no null values, so, the given above SQL query will show the below result.
24). DROP
The SQL DROP keyword can be used to delete a database, table, view, column, index, etc.
25). DROP COLUMN
We can delete an existing column in a table by using a SQL DROP COLUMN along with an ALTER statement. Furthermore, let us delete column PHONE by using the given below SQL query.
1 |
ALTER TABLE CUSTOMERS DROP COLUMN PHONE; |
We can see that in the above result, the PHONE column is dropped.
26). DROP DATABASE
A database in SQL can be deleted by using the SQL DROP DATABASE statement.
1 |
DROP DATABASE DATABASE_NAME; |
27). DROP TABLE
Therefore, a table in SQL can be deleted by using a SQL DROP TABLE statement.
1 |
DROP TABLE TABLE_NAME; |
We can delete the table CUSTOMERS by using the SQL DROP TABLE keyword as given below.
But here, we need to be careful while using SQL DROP TABLE as it will remove the table definition along with all the data and indexes, etc.
28). GROUP BY
This is used along with the SQL aggregate functions like COUNT, MAX, MIN, AVG, SUM, etc., and groups the result set. The given below query will group the CUSTOMER_ID according to the various CITY.
1 |
SELECT COUNT(CUSTOMERS_ID),CITY FROM CUSTOMERS GROUP BY CITY; |
29). HAVING
This Having keyword in SQL is used with aggregate functions and GROUP BY instead of the SQL WHERE clause to filter the values of a result set.
1 |
SELECT COUNT(CUSTOMERS_ID),CITY FROM CUSTOMERS GROUP BY CITY HAVING COUNT(CUSTOMERS_ID)>=1; |
The given above SQL query will filter the result set by displaying only those values that satisfy the condition given within the HAVING clause
The above result set shows that the values for which the count of the customer_id is more than 1.
30). IN
The SQL IN keyword is used within a SQL WHERE clause to specify more than 1 value or in other words, we can say that it can be used instead of the usage of multiple OR keywords in a query.
The given below SQL query will select the records for the City Kanpur, Lucknow, and Ghaziabad by the use of the SQL IN keyword.
1 |
SELECT * FROM CUSTOMERS WHERE CITY IN ('Lucknow','Noida','Ghaziabad'); |
The given above result set shows the usage of IN SQL keyword which selects the records only for the CITY specified within the IN clause.
31). JOIN
The SQL JOIN keyword is used to combine the rows between two or more tables with related columns among the tables. Furthermore, the SQL JOIN can be INNER, LEFT, RIGHT, OUTER JOIN, etc.
Furthermore, lets us take another table ‘CUSTOMERS_ORDER’
as an example.
We can perform an SQL inner join of the CUSTOMERS
and CUSTOMERS_ORDER
tables as below.
1 2 3 |
SELECT CUSTOMERS.CUSTOMERS_NAME, CUSTOMERS.CITY, CUSTOMERS_ORDER.PRODUCT_NAME FROM CUSTOMERS INNER JOIN CUSTOMERS_ORDER ON CUSTOMERS.CUSTOMERS_ID =CUSTOMERS_ORDER.ORDER_ID; |
The given above SQL query will join the two tables CUSTOMERS and CUSTOMERS_ORDER on the columns CUSTOMERS_ID and ID and display only the values which are present in both the tables.
The above result screenshot shows the matching records for customers_id 111 and 161. which are common in both the tables. But the other customers_ids are excluded as they are not present in the CUSTOMERS_ORDER table. Therefore, similarly, the other SQL JOINS can be performed.
32). UNION
The SQL UNION keyword is utilized in SQL to combine the distinct values of two or more select statements.
1 |
SELECT CUSTOMERS_ID FROM CUSTOMERS UNION SELECT ORDER_ID FROM CUSTOMERS_ORDER; |
The given above SQL query will show the below result.
33). UNION ALL
This keyword in SQL combines two or more select statements but it allows duplicate values.
1 |
SELECT CUSTOMERS_ID FROM CUSTOMERS UNION ALL SELECT ORDER_ID FROM CUSTOMERS_ORDER; |
The given above SQL query will show the below result.
The given above result shows that SQL UNION ALL allows duplicate values which would not be present in the case of UNION.
34). EXISTS
Furthermore, the keyword EXISTS checks if a certain record exists in a sub-query
1 |
SELECT CUSTOMERS_NAME FROM CUSTOMERS WHERE EXISTS (SELECT PRODUCT_NAME FROM CUSTOMERS_ORDER WHERE CUSTOMERS_ID = ORDER_ID); |
The given above SQL query will return true as the sub-query returns the below values.
35). LIKE
This SQL keyword is used for the purpose of searching along with a WHERE clause
for a particular pattern. Furthermore, Wildcard %
is used to search for a pattern.
In the given below SQL query let us search for a pattern ‘ad’
which occurs in the column ‘CUSTOMERS_NAME’
.
1 |
SELECT CUSTOMERS_NAME FROM CUSTOMERS WHERE CUSTOMERS_NAME LIKE '%Ad'; |
36). CASE
This SQL keyword is used to display different outputs according to different conditions.
1 2 3 4 5 |
SELECT CUSTOMERS_ID, CUSTOMERS_NAME, CASE WHEN CITY = 'Lucknow' THEN "City is Lucknow" ELSE "City is NOT Lucknow" END AS Output FROM CUSTOMERS; |
A few other keywords are DEFAULT used to provide a default value for a column, Furthermore, The SQL UNIQUE is used to ensure all the values in a column are unique, etc.
Related Question And Answer (FAQs)
What are keywords in SQL?
Answer: The keywords in SQL are those that are reserved words that are used for the purpose of performing various operations in the database. Therefore, there are many keywords in SQL and as SQL is case insensitive, it doesn’t matter if we use for example SELECT or select.
How do you use keywords in SQL query?
Answer: The keyword here is reserved word in SQL. suppose that if you ARE using SQL Server, then you can just simply wrap the square brackets around the column or table name. Furthermore, I even have additionally sweet-faced this issue. and also the resolution for this is to put [Column_Name] like this in the query.
What are database keywords?
Keywords, also commonly known as search terms, are the words that you just enter into the database search boxes. Furthermore, they represent the main concepts or most ideas of your research topic and are the words used in everyday life to describe and explain the topic.
Is a reserved SQL keyword?
Reserved words are SQL keywords and other different symbols that have special meanings when they are processed by the Relational Engine. Therefore, the Reserved words aren’t recommended for use as a database, table, column, variable, or other object names.
Conclusion
You have learned in this article, SQL Keywords, the various keywords in SQL basically provide flexibility in designing a database, tables, etc. They provide the designer with several features which become most useful while making any changes after the design is completed. I hope you will enjoy it!