How to use Aliases in SQL

How to use Aliases in SQL


How to use Aliases in SQL

Learn how to use aliases in SQL with easy to follow examples. This tutorial explains what an alias is and how to create an alias for a column name as well as how to alias table names.

SQL Aliases (and sample data):
https://www.techonthenet.com/sql/alia

SQL Tutorials:
https://www.techonthenet.com/sql/inde

Don’t forget to subscribe to our channel for more great SQL tutorials!

Visit us at: https://www.techonthenet.com
or follow us on:

Facebook: https://www.facebook.com/techonthenetcom
Twitter: https://twitter.com/tech_on_the_net
Pinterest: https://www.pinterest.com/techonthene


Content

0.03 -> aliases are used in SQL to create a
2.76 -> temporary name for a column or table
5.42 -> the syntax for aliasing columns as well
8.73 -> as tables is the same you simply type
11.7 -> the column or table name followed by the
14.009 -> as keyword and then the alias name in
16.97 -> some databases you can omit the as
19.439 -> keyword but for the purposes of this
21.689 -> tutorial we will define our aliases
23.82 -> using as so let's start by looking at
27.63 -> how to alias a column name generally
30.9 -> aliases are used to make the column
32.73 -> headings and your result set easier to
34.8 -> read most commonly you might alias a
38.07 -> column when using an aggregate function
39.75 -> such as min max average sum or count in
44.129 -> your query so let's get started here we
48.3 -> have a table called employees containing
50.309 -> the following data let's write a select
53.309 -> statement that counts the number of
54.75 -> employees in each department in order to
58.05 -> do this you will need to use the count
59.64 -> function
63.87 -> we'll start by entering select
65.97 -> Department ID comma count asterisk from
69.84 -> employees group ID Department ID
74.45 -> currently we aren't using any aliases in
77.18 -> this SQL statement but we want to show
79.64 -> you what the result set looks like to
81.68 -> help you better understand why you might
83.36 -> want to use a column alias when we run
86.09 -> the Select statement we will get two
87.68 -> columns and a result set the first
90.02 -> column is the department ID and the
92.27 -> second column is the results of the
93.979 -> count function that displays the number
96.17 -> of employees in that department notice
98.96 -> that the column heading in the results a
100.82 -> count asterisk let's use an alias to
103.52 -> rename the second column heading to
105.5 -> something more meaningful so we'll go
107.63 -> back to our original select statement
109.31 -> and after the count asterisk we will
111.619 -> enter as total now when we rerun this
114.71 -> query our column heading will say total
117.14 -> instead of count asterisk now before
120.5 -> moving on let's look at how to create a
122.24 -> column alias that contain spaces let's
125.119 -> rewrite our query and include a space in
127.549 -> the column alias again we will enter
130.1 -> select department ID comma count
132.59 -> asterisk but this time we will enclose
134.93 -> our alias name in quotes so we will
137.33 -> enter as quote total employees quote
140.079 -> notice the space between the words total
142.76 -> and employees and then we'll finish the
145.19 -> rest of the SQL statement as before
149.48 -> when this query is run total employees
152.33 -> will become the heading for the second
153.95 -> column in a result set so the general
156.739 -> rule here is that if your alias contains
159.17 -> a space you must enclose the alias name
161.33 -> in quotes however it is okay to enclose
164.42 -> alias name in quotes even if there are
166.489 -> no spaces it's your choice now let's
169.55 -> move on and look at how to alias a table
171.65 -> name when you alias a table it is either
174.44 -> because you plan to list the same table
176.33 -> more than once in the from clause in
178.58 -> other words a self join or you want to
181.88 -> shorten the table name to make the SQL
183.92 -> statement shorter and easier to read
186.04 -> let's start with two tables products and
189.38 -> categories we'll write a query that
192.079 -> joins these two tables together and uses
194.84 -> aliases for the table names we'll use
197.51 -> the first letter of the table name as
199.19 -> the alias name so for example we will
201.829 -> use an alias of P for the products table
204.049 -> and an alias of C for the categories
206.72 -> table we will start by entering select P
209.72 -> dot product name comma C dot category
213.049 -> name since our tables are now called P
215.84 -> for the products table and C for the
218.18 -> categories table you will use the alias
220.88 -> name instead of the original table name
223.069 -> when referring to these tables in the
225.2 -> query next we will enter from products
227.9 -> as P this defines P as the alias for the
230.93 -> products table let's enter join to the
233.69 -> categories table by entering inner join
236.09 -> categories as C on P category ID equal C
241.609 -> category ID
244.25 -> and let's apply a where clause by
246.32 -> entering where P dot product name not
249.44 -> equal to pair in single quotes when we
252.44 -> run this query we will get five records
254.3 -> in our result set in this example we
257.15 -> Elias both of our tables but it is
259.43 -> important to mention that if you do
261.23 -> choose to create table aliases you do
263.87 -> not have to create aliases for all of
265.58 -> the tables listed in your from clause
267.26 -> you can choose to create aliases on any
269.99 -> or all of the tables this covers a few
273.44 -> examples of how to create column and
275.36 -> table aliases if you would like to see
278.06 -> more examples or would like to download
280.04 -> the sample date that we use for this
281.54 -> tutorial please visit our website at
283.79 -> tech on the net comm you can also try
286.34 -> the examples in our SQL editor on our
288.59 -> website look for the try it button next
291.08 -> to each example if you found our
293.81 -> tutorial helpful please leave a like on
295.85 -> this video and don't forget to subscribe
297.65 -> to our youtube channel for more great
299.84 -> SQL tutorials

Source: https://www.youtube.com/watch?v=ngPeJ06f4-A