W3. SQL Aliases Flashcards

1
Q

Q: What is an SQL alias?

A

A: It’s a temporary name given to a table or column in a query to make names more readable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: How do you create an alias in SQL?

A

A: Use the AS keyword, e.g., SELECT CustomerID AS ID FROM Customers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: Is the AS keyword mandatory for creating aliases?

A

A: No, in most databases, you can omit AS, e.g., SELECT CustomerID ID FROM Customers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: Write the syntax for creating an alias for a column.

A

SELECT column_name AS alias_name
FROM table_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: Write the syntax for creating an alias for a table.

A

SELECT column_name(s)
FROM table_name AS alias_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: Write a query to create aliases for CustomerID as “ID” and CustomerName as “Customer.”

A

SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: How can you create an alias with spaces in it?

A

A: Surround the alias with square brackets or double quotes, e.g., AS [My Great Products] or AS “My Great Products”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: Write a query to alias ProductName as “My Great Products” using square brackets.

A

SELECT ProductName AS [My Great Products]
FROM Products;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: How do you concatenate columns into a single alias?

A

A: Use the + operator (or CONCAT in MySQL), e.g., SELECT CONCAT(Address, ‘, ‘, PostalCode, ‘, ‘, City, ‘, ‘, Country) AS Address.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: Write a query to concatenate Address, PostalCode, City, and Country as “Address” (for MySQL).

A

SELECT CustomerName, CONCAT(Address, ‘, ‘, PostalCode, ‘, ‘, City, ‘, ‘, Country) AS Address
FROM Customers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: How can aliases simplify SQL queries with multiple tables?

A

A: They make SQL statements shorter and more readable, especially when joining multiple tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: Write a query to join Customers and Orders using aliases “c” and “o” to select orders from “Around the Horn”.

A

SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName = ‘Around the Horn’ AND c.CustomerID = o.CustomerID;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: When are aliases particularly useful?

A

A: When multiple tables are involved, functions are used, column names are long, or columns are combined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly