How to Use Reserved Words as Column Names in SQL?
Last Updated :
28 Oct, 2021
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.
Similar Reads
How to Search For Column Names in SQL? In SQL, sometimes we need to search the column names in a table using the prefixes. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks. Query: CREATE DATABA
2 min read
How to Rename a Column in PL/SQL? Renaming a column in PL/SQL is a fundamental operation in Oracle Database management. It enhances clarity, maintains consistency, or accommodates evolving data requirements. Database administrators can ensure the data integrity and process of streamlining data manipulation by altering the column nam
4 min read
How to Change a Column Name in SQL? The ALTER TABLE statement in SQL is a powerful command used to modify the structure of an existing table without affecting its data. It enables changes like adding, dropping, renaming or altering columns in the table. Among these operations, altering a column with the CHANGE or RENAME command is com
3 min read
How to Use Column Alias in SELECT Statement? When working with SQL queries, readability and clarity are crucial for efficient data analysis. Using column aliases in the SELECT statement can significantly improve the clarity of our output by providing more meaningful and user-friendly names to the columns. Aliases are especially useful when wor
3 min read
SQL Query to Get Column Names From a Table SQL stands for Structured Query Language. It is a language used to interact with the database, i.e to create a database, to create a table in the database, to retrieve data or update a table in the database, etc. SQL is an ANSI(American National Standards Institute) standard. Using SQL, we can do ma
2 min read