MySQL AS Clause

MySQL | AS Clause: Learn about the MySQL AS Clause, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 08, 2022

AS Clause

AS clause is one of the unique clauses because it helps us to give a temporary name to any table or column. Isn't it awesome? It is, sometimes when we write big queries then in return we simply get results with no column heads but if this clause is used with them then you can give a different name to the column.

AS Clause Syntax

SELECT Column_name AS temp_name
FROM table_name;

Let us understand this in detail;

AS Clause Examples

Case 1: A query without using AS clause

Suppose we have a table named "student_details" and inside this table, we have the following columns;

AS Clause (Example 1)

Now if I don't use AS clause, our result will look like this;

AS Clause (Example 2)

As you can according to the query written, it simply gives the sum of all the MARKS, but see the column name, does it look nice? Not, to improve this we use AS clause, now we'll see how we can rename this using AS clause.

Case 2: A query using AS clause

If we will hit the above query with AS clause, our result will look like this;

SELECT sum (Marks) AS total_marks 
FROM student_details;
AS Clause (Example 3)

After renaming the result using AS clause our result looks better.

Case 3: Using AS clause more than one time

Suppose we want to find out the average, sum, and count of the entire student in the table named "student_details" such case our query will be;

SELECT avg(Marks) AS average_marks, count(ID) AS total_students, sum(Marks) AS total_Marks 
FROM student_details;
AS Clause (Example 4)

As you can see in one query we can use AS clause more than one time, you just have to separate them with commas.

Points to keep in mind for using AS clause

  • AS clause is also known as the alias name.
  • The name you give using AS clause is temporary; it is not permanent.
  • Whenever you write a temporary name using AS clauses do not write spaces in them, else it will show you the error and your query won't run. In spite try underscore (_) in place of space (as I did above).



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.