Friday, September 22, 2023

What is the SQL keyword for renaming a column in a result set?

In SQL, you can use the AS keyword to rename a column in a result set. This is often referred to as "aliasing" a column. The AS keyword allows you to provide a custom name for a column in the result set, making the output more descriptive or user-friendly.

Here's the syntax for renaming a column using the AS keyword:

SELECT column_name AS new_column_name 
FROM table_name;
  • column_name: The name of the column you want to include in the result set.
  • new_column_name: The custom name you want to assign to the column in the result set.

For example, if you have a table named "Employees" with a column named "EmployeeName," and you want to rename it to "Full Name" in the result set, you can use the AS keyword like this:

SELECT EmployeeName AS "Full Name" 
FROM Employees;

Executing this query will include the "Full Name" column in the result set, with data from the "EmployeeName" column, and it will be displayed as "Full Name" in the output. The use of double quotes around the new column name is optional but can be used to specify column names with spaces or special characters. 

No comments:

Post a Comment

If you have any doubts. Please let me know

How can you create an alias for a table in a SQL query?

In SQL, you can create an alias for a table in a query to give the table a temporary, alternative name that you can use in the query. Table ...