{"id":506,"date":"2016-04-08T15:15:22","date_gmt":"2016-04-08T15:15:22","guid":{"rendered":"https:\/\/sqltutorial.org\/?page_id=506"},"modified":"2025-02-04T07:24:42","modified_gmt":"2025-02-04T14:24:42","slug":"sql-alias","status":"publish","type":"page","link":"https:\/\/www.sqltutorial.org\/sql-alias\/","title":{"rendered":"SQL Alias"},"content":{"rendered":"\r\n<p><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><strong>Summary<\/strong>: in this tutorial, you will learn about SQL aliases, including table and column aliases, to make your queries shorter and more understandable.<\/span><\/p>\r\n\r\n\r\n\r\n<p>SQL alias allows you to assign a table or a column a temporary name during the execution of a <a href=\"https:\/\/www.sqltutorial.org\/sql-select\/\">query<\/a>. SQL has two types of aliases: table and column aliases.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='sql-column-aliases'>SQL column aliases <a href=\"#sql-column-aliases\" class=\"anchor\" id=\"sql-column-aliases\" title=\"Anchor for SQL column aliases\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>When designing database tables, you may use abbreviations for the column names to keep them short. For example:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>The <code>so_no<\/code> stands for sales order number.<\/li>\r\n\r\n\r\n\r\n<li>The <code>qty<\/code> stands for quantity.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>Or you may have to work with legacy systems that aggressively use abbreviations for naming columns and tables.<\/p>\r\n\r\n\r\n\r\n<p>For example, SAP ERP uses abbreviations (in German) to name all the columns and tables such as <code>VBELN<\/code> for naming sales document number column.<\/p>\r\n\r\n\r\n\r\n<p>When you use SQL to query data from these tables, the output is not obvious. To fix it, you can use column aliases that give columns temporary names during the execution of the query.<\/p>\r\n\r\n\r\n\r\n<p>The following shows the syntax of using column aliases:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">column_name AS alias_name<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>In this syntax, you specify the column alias after the <code>AS<\/code> keyword followed by the column name. The <code>AS<\/code> keyword is optional. So you can omit it like this:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">column_name alias_name<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>If the alias contains spaces, you need to put it inside single quotes (or double quotes) like this:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">column_name AS 'Alias Name'<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>The following example shows how to use the column aliases:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\r\n  inv_no <span class=\"hljs-keyword\">AS<\/span> invoice_no,\r\n  amount,\r\n  due_date <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-string\">'Due date'<\/span>,\r\n  cust_no <span class=\"hljs-string\">'Customer No'<\/span>\r\n<span class=\"hljs-keyword\">FROM<\/span>\r\n  invoices;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>This query has multiple column aliases:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>The <code>invoice_no<\/code> is the column alias of the <code>inv_no<\/code> column.<\/li>\r\n\r\n\r\n\r\n<li>The <code>'Due date'<\/code> is the column alias of the <code>due_date<\/code> column. Because the alias contains space, you have to place it inside a single quote (&#8216;) or double quotes (&#8220;) .<\/li>\r\n\r\n\r\n\r\n<li>The <code>'Customer no'<\/code> is the alias of the <code>cust_no<\/code> column. Note that it doesn&#8217;t have the <code>AS<\/code> keyword.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id='aliases-for-expressions'>Aliases for expressions <a href=\"#aliases-for-expressions\" class=\"anchor\" id=\"aliases-for-expressions\" title=\"Anchor for Aliases for expressions\">#<\/a><\/h3>\r\n\r\n\r\n\r\n<p>If a query contains expressions, you can assign column aliases to the expressions. For example:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\r\n  first_name,\r\n  last_name,\r\n  salary * <span class=\"hljs-number\">1.1<\/span> <span class=\"hljs-keyword\">AS<\/span> new_salary\r\n<span class=\"hljs-keyword\">FROM<\/span>\r\n  employees;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGZpcnN0X25hbWUsIGxhc3RfbmFtZSwgc2FsYXJ5ICogMS4xIEFTIG5ld19zYWxhcnkgRlJPTSBlbXBsb3llZXM7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\r\n\r\n\r\n\r\n<p>In this example, the database will use the column aliases as the heading of the expressions in the result set.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id='common-mistakes-of-column-aliases'>Common mistakes of column aliases <a href=\"#common-mistakes-of-column-aliases\" class=\"anchor\" id=\"common-mistakes-of-column-aliases\" title=\"Anchor for Common mistakes of column aliases\">#<\/a><\/h3>\r\n\r\n\r\n\r\n<p>Since you assign aliases to columns in the <code>SELECT<\/code> clause, you can only reference the aliases in the clauses that are evaluated after the <code>SELECT<\/code> clause.<\/p>\r\n\r\n\r\n\r\n<p>The following query will result in an error:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\r\n  first_name,\r\n  last_name,\r\n  salary * <span class=\"hljs-number\">1.1<\/span> <span class=\"hljs-keyword\">AS<\/span> new_salary\r\n<span class=\"hljs-keyword\">FROM<\/span>\r\n  employees\r\n<span class=\"hljs-keyword\">WHERE<\/span>\r\n  new_salary &gt; <span class=\"hljs-number\">5000<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Error:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Unknown column <span class=\"hljs-string\">'new_salary'<\/span> <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-string\">'where clause'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Why?<\/p>\r\n\r\n\r\n\r\n<p>In this <code>SELECT<\/code> statement, the database evaluates the clauses in the following order:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">FROM &gt; WHERE &gt; <span class=\"hljs-keyword\">SELECT<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>The database evaluates the <code><a href=\"https:\/\/www.sqltutorial.org\/sql-where\/\">WHERE<\/a><\/code> clause before the <code>SELECT<\/code> clause. Therefore, at the time it evaluates the <code>WHERE<\/code> clause, the database doesn&#8217;t have the information of the <code>new_salary<\/code> column alias. So it issued an error.<\/p>\r\n\r\n\r\n\r\n<p>However, the following query works correctly:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\r\n  first_name,\r\n  last_name,\r\n  salary * <span class=\"hljs-number\">1.1<\/span> <span class=\"hljs-keyword\">AS<\/span> new_salary\r\n<span class=\"hljs-keyword\">FROM<\/span>\r\n  employees\r\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\r\n  new_salary;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGZpcnN0X25hbWUsIGxhc3RfbmFtZSwgc2FsYXJ5ICogMS4xIEFTIG5ld19zYWxhcnkgRlJPTSBlbXBsb3llZXMgT1JERVIgQlkgbmV3X3NhbGFyeTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\r\n\r\n\r\n\r\n<p>In this example, the database evaluates the clauses of the query in the following order:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">FROM &gt; <span class=\"hljs-keyword\">SELECT<\/span> &gt; <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>The database evaluates the <code>SELECT<\/code> clause before the <code><a href=\"https:\/\/www.sqltutorial.org\/sql-order-by\/\">ORDER BY<\/a><\/code> clause. Therefore, at the time of evaluating the <a href=\"https:\/\/www.sqltutorial.org\/sql-order-by\/\"><code>ORDER BY<\/code> <\/a>clause, the database has the information of the <code>new_salary<\/code> alias, which is the alias of the expression <code>salary * 1.1<\/code>. Hence, it works as expected.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='sql-table-aliases'>SQL table aliases <a href=\"#sql-table-aliases\" class=\"anchor\" id=\"sql-table-aliases\" title=\"Anchor for SQL table aliases\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>Like columns, you can assign aliases to tables. These aliases are called table aliases.<\/p>\r\n\r\n\r\n\r\n<p>To assign an alias to a table, you use the following syntax:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">table_name <span class=\"hljs-keyword\">AS<\/span> table_alias<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>In this syntax, the <code>AS<\/code> keyword is also optional. So you can omit it like the following:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">table_name table_alias<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>Assigning an alias to a table does not rename the table permanently. It just gives the table another name temporarily during the execution of a query.<\/p>\r\n\r\n\r\n\r\n<p>So why do you need table aliases?<\/p>\r\n\r\n\r\n\r\n<p>When specifying the column names in the <code>SELECT<\/code> clause, you can use the following syntax:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">table_name<\/span><span class=\"hljs-selector-class\">.column_name<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>In this syntax, the column has a fully qualified name that includes both table and column names. For example:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\r\n  employees.first_name,\r\n  employees.last_name\r\n<span class=\"hljs-keyword\">FROM<\/span>\r\n  employees;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGVtcGxveWVlcy5maXJzdF9uYW1lLCBlbXBsb3llZXMubGFzdF9uYW1lIEZST00gZW1wbG95ZWVzOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\r\n\r\n\r\n\r\n<p>In this example, the query selects the data from the <code>first_name<\/code> and <code>last_name<\/code> columns of the <code>employees<\/code> table. Unlike a typical query, we specify the fully qualified names for the <code>first_name<\/code> and <code>last_name<\/code> columns.<\/p>\r\n\r\n\r\n\r\n<p>In the <code>SELECT<\/code> clause, instead of using the <code>employees<\/code> table name, you can use the table alias of the <code>employees<\/code> table. For example:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\r\n  e.first_name,\r\n  e.last_name\r\n<span class=\"hljs-keyword\">FROM<\/span>\r\n  employees <span class=\"hljs-keyword\">AS<\/span> e;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGUuZmlyc3RfbmFtZSwgZS5sYXN0X25hbWUgRlJPTSBlbXBsb3llZXMgQVMgZTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\r\n\r\n\r\n\r\n<p>In this example, we assign <code>e<\/code> table alias to the <code>employees<\/code> in the <code>FROM<\/code> clause and reference it in the <code>SELECT<\/code> clause.<\/p>\r\n\r\n\r\n\r\n<p>But why do you need to use the fully qualified names for columns?<\/p>\r\n\r\n\r\n\r\n<p>See the following <code>employees<\/code> and <code>departments<\/code> tables from the <a href=\"https:\/\/www.sqltutorial.org\/sql-sample-database\/\">sample database<\/a>:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"451\" height=\"269\" class=\"wp-image-1190\" src=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2018\/01\/employees_departments_tables.png\" alt=\"Employees &amp; Departments Tables\" srcset=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2018\/01\/employees_departments_tables.png 451w, https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2018\/01\/employees_departments_tables-300x179.png 300w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Both <code>employees<\/code> and <code>departments<\/code> tables have the columns with the same name: department_id.<\/p>\r\n\r\n\r\n\r\n<p>When querying data from both tables, you need to specify the exact table that the <code>department_id<\/code> column belongs to. Otherwise, you&#8217;ll get an error because the database doesn&#8217;t know which table it needs to go to select data.<\/p>\r\n\r\n\r\n\r\n<p>If you want to select data from the <code>employees<\/code> table, you can reference it in the <code>SELECT<\/code> clause as follows:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">employees.department_id<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>The same is applied to the <code>department_id<\/code> of the <code>departments<\/code> table:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">departments.department_id<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>If <code>e<\/code> and <code>d<\/code> are table aliases of the <code>employees<\/code> and <code>departments<\/code> tables, you can reference the <code>department_id<\/code> column in each table using the table aliases as follows:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">e.department_id\r\nd.department_id<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>In the next tutorial, you&#8217;ll learn how to use the join clauses to select data from both tables and apply the table aliases. Also, you&#8217;ll learn how to reference the same table in a single query twice using the <a href=\"https:\/\/www.sqltutorial.org\/sql-self-join\/\">self-join<\/a> technique. In this case, you need to use the table aliases.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>SQL has two types of aliases: column and table aliases.<\/li>\r\n\r\n\r\n\r\n<li>Reference the column aliases in the clauses that are evaluated after the <code>SELECT<\/code> clause.<\/li>\r\n\r\n\r\n\r\n<li>Use table aliases to qualify the column names.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='databases'>Databases <a href=\"#databases\" class=\"anchor\" id=\"databases\" title=\"Anchor for Databases\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><a href=\"https:\/\/www.oracletutorial.com\/oracle-basics\/oracle-alias\/\" target=\"_blank\" rel=\"noreferrer noopener\">Oracle Alias<\/a><\/li>\r\n\r\n\r\n\r\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-alias\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQL Server Alias<\/a><\/li>\r\n\r\n\r\n\r\n<li><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-alias\/\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL Alias<\/a><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p><iframe loading=\"lazy\" class=\"iframe\" src=\"\/quiz\/?quiz=alias\" name=\"quiz\" width=\"600\" height=\"700\"><\/iframe><\/p>\r\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"506\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-alias\/\"\n\t\t\t\tdata-post-title=\"SQL Alias\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"506\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-alias\/\"\n\t\t\t\tdata-post-title=\"SQL Alias\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn about SQL aliases, including table and column aliases, to make your queries shorter and more understandable. SQL alias allows you to assign a table or a column a temporary name during the execution of a query. SQL has two types of aliases: table and column aliases. SQL column [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":61,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-506","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Alias<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use SQL alias including column and table aliases to make your queries shorter and more understandable.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqltutorial.org\/sql-alias\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Alias\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use SQL alias including column and table aliases to make your queries shorter and more understandable.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqltutorial.org\/sql-alias\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-04T14:24:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2018\/01\/employees_departments_tables.png\" \/>\n\t<meta property=\"og:image:width\" content=\"451\" \/>\n\t<meta property=\"og:image:height\" content=\"269\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-alias\/\",\"url\":\"https:\/\/www.sqltutorial.org\/sql-alias\/\",\"name\":\"SQL Alias\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltutorial.org\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-alias\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-alias\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2018\/01\/employees_departments_tables.png\",\"datePublished\":\"2016-04-08T15:15:22+00:00\",\"dateModified\":\"2025-02-04T14:24:42+00:00\",\"description\":\"This tutorial shows you how to use SQL alias including column and table aliases to make your queries shorter and more understandable.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-alias\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqltutorial.org\/sql-alias\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-alias\/#primaryimage\",\"url\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2018\/01\/employees_departments_tables.png\",\"contentUrl\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2018\/01\/employees_departments_tables.png\",\"width\":451,\"height\":269,\"caption\":\"Employees & Departments Tables\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-alias\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqltutorial.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Alias\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqltutorial.org\/#website\",\"url\":\"https:\/\/www.sqltutorial.org\/\",\"name\":\"SQL Tutorial\",\"description\":\"An Interactive SQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqltutorial.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Alias","description":"This tutorial shows you how to use SQL alias including column and table aliases to make your queries shorter and more understandable.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqltutorial.org\/sql-alias\/","og_locale":"en_US","og_type":"article","og_title":"SQL Alias","og_description":"This tutorial shows you how to use SQL alias including column and table aliases to make your queries shorter and more understandable.","og_url":"https:\/\/www.sqltutorial.org\/sql-alias\/","og_site_name":"SQL Tutorial","article_modified_time":"2025-02-04T14:24:42+00:00","og_image":[{"width":451,"height":269,"url":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2018\/01\/employees_departments_tables.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqltutorial.org\/sql-alias\/","url":"https:\/\/www.sqltutorial.org\/sql-alias\/","name":"SQL Alias","isPartOf":{"@id":"https:\/\/www.sqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqltutorial.org\/sql-alias\/#primaryimage"},"image":{"@id":"https:\/\/www.sqltutorial.org\/sql-alias\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2018\/01\/employees_departments_tables.png","datePublished":"2016-04-08T15:15:22+00:00","dateModified":"2025-02-04T14:24:42+00:00","description":"This tutorial shows you how to use SQL alias including column and table aliases to make your queries shorter and more understandable.","breadcrumb":{"@id":"https:\/\/www.sqltutorial.org\/sql-alias\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqltutorial.org\/sql-alias\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqltutorial.org\/sql-alias\/#primaryimage","url":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2018\/01\/employees_departments_tables.png","contentUrl":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2018\/01\/employees_departments_tables.png","width":451,"height":269,"caption":"Employees & Departments Tables"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqltutorial.org\/sql-alias\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"SQL Alias"}]},{"@type":"WebSite","@id":"https:\/\/www.sqltutorial.org\/#website","url":"https:\/\/www.sqltutorial.org\/","name":"SQL Tutorial","description":"An Interactive SQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/comments?post=506"}],"version-history":[{"count":0,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/506\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/media?parent=506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}