SQL AS Operator (Aliasing)
It is the SQL operator that we use to facilitate the use of long and difficult to use column names while making SQL queries and to make our work practical. With this operator we also get more readable tables. The point to note here is that we do not change the column names, we only use these aliases when displaying them to the user. So we do not make any changes to the tables. Let's show it with an example.
In our previous lessons, we created a database called
baransel and a table called
students. Let's show this table now.
SELECT * FROM students
If you pay attention, the column names can hardly be remembered and are not usable and also not readable. Now let's create easier to use and readable column names.
For this we will use the keyword
SELECT name AS "Student Name" FROM students
As you can see, we have shown the column named
name with the alias
Student Name. Are you going to say that we can only make one column one by one? Of course not, you can call the column you want with its alias.
SELECT name AS "Student Name", surname as "Student Surname" FROM students
As you can see, we called multiple columns with aliases. Another issue we need to pay attention to here is that we can no longer call all columns with the
* operator. For this, we have to write them all one by one, let's do it right away.
SELECT name AS "Student Name", surname as "Student Surname", quiz, midterm, final, class_no FROM students
In this way, we have called all the columns. If you notice here, the aliases for the ones we called with aliases came, while the other columns came normally.
SQL Join Columns
Here, the names and surnames of the students are in different columns, but you may have seen that the names and surnames are used together in many places in daily life.
This is because it makes it more useful and readable. Now let's do it this way. Here again, the point we will pay attention to is that we do not change the table, we only convert it to data in a format that the user can understand.
SELECT CONCAT(name ," ", surname) FROM students
Thus, we have combined the two columns. But if you pay attention, there is no column name, you will ask how we will handle it.
SELECT CONCAT(name ," ", surname) AS "Full name" FROM students
In this way, we now have more readable tables. If we want to show all the columns then we have to write them all one by one.
Top comments (0)