Open In App

How to Use Reserved Words as Column Names in SQL?

Improve
Improve
Like Article
Like
Save
Share
Report

In SQL, certain words are reserved. These are called Keywords or Reserved Words. These words cannot be used as identifiers i.e. as column names in SQL. But, there is an exception to this rule too. In this article, we will discuss how to use Reserved Words as column names in SQL. For this article, we will be using the Microsoft SQL Server as our database.

Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks

Query:

CREATE DATABASE GeeksForGeeks

Output:

Step 2: Use the GeeksForGeeks database. For this use the below command

Query:

USE GeeksForGeeks

Output:

Step 3: Create a table inside the database GeeksForGeeks. We will name the table BIKE. It will contain 2 columns named SELECT and TABLE. Both these are keywords in SQL. The trick here is to type the column names inside the square brackets ‘[]’ so that they are not read as a Reserved Word by the compiler.

Query:

CREATE TABLE BIKE(
[SELECT] VARCHAR(10),
[TABLE] INT);

Output:

Note: Here, the SELECT column stores the names of the bikes, and the TABLE column stores the cost of the bikes.

Step 4: Add data to the BIKE table.

Query:

INSERT INTO BIKE VALUES('HERO',10000);
INSERT INTO BIKE VALUES('TVS',20000);
INSERT INTO BIKE VALUES('YAMAHA',30000);
INSERT INTO BIKE VALUES('HONDA',40000);
INSERT INTO BIKE VALUES('BAJAJ',50000);

Output:

Step 4: Display the SELECT column from the BIKE table.

Query:

SELECT [SELECT] FROM BIKE;

Output:

Step 5: Display the TABLE column from the BIKE table.

Query:

SELECT [TABLE] FROM BIKE;

Output:

Step 5: We can even display both the columns using the comma separator.

Query:

SELECT [SELECT],[TABLE] FROM BIKE;

Output:

Hence, in a similar fashion, we can use any of the reserved names in SQL as a column name.


Last Updated : 28 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads