You can choose to create aliases on any or all of the tables.įor example, we could modify our example above and create an alias for the departments table as well. When creating table aliases, it is not necessary to create aliases for all of the tables listed in the FROM clause. Now within this SQL statement, we can refer to the employees table as e. In this example, we've created an alias for the employees table called e. ORDER BY e.last_name DESC, partment_name ASC Let's look at an example of how to alias a table name in SQLite.įor example: SELECT e.employee_id, e.last_name, partment_name When you create an alias on a table, it is either because you plan to list the same table name more than once in the FROM clause (ie: self join), or you want to shorten the table name to make the SQL statement shorter and easier to read. Since there are spaces in this alias_name, "largest ID" must be enclosed in quotes. In this example, we've aliased the MAX(employee_id) field as "largest ID". Next, let's look at an example where we are required to enclose the alias_name in quotes.įor example: SELECT department_id, MAX(employee_id) AS "largest ID" However, it would have been perfectly acceptable to write this example using quotes as follows: SELECT department_id, MAX(employee_id) AS "largest" Because our alias_name did not include any spaces, we are not required to enclose the alias_name in quotes. As a result, largest will display as the heading for the second column when the result set is returned. ![]() In this example, we've aliased the MAX(employee) field as largest. For example, when using the MAX function, you might alias the result of the MAX function in SQLite.įor example: SELECT department_id, MAX(employee_id) AS largest Generally, aliases are used to make the column headings in your result set easier to read.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |