views:

114

answers:

7

Describe the output of the following SQL query:

select custId, name
from customer
where region = "New York"
UNION 
select cust.custId, cust.name
from customer cust
where cust.custId IN (select cust_order.custId
from customer_order cust_order, company_employee comp_emp
where cust_order.salesEmpId = comp_emp.empId
AND comp_emp.name = 'DANIEL');

My question is: at the line which contains from customer cust is 'cust' referring to a column in the customer table?

This is a homework question I have identified the components leading up to this line and I think that cust is a column in the customer table... I am not asking for an overall solution just a bit of encouragement if I am on the right track...

+1  A: 

Cust is an alias on the customer table. It is used to enable you to not have to spell out the whole table name everywhere.

HLGEM
Thank you for your input it is appreciated!sammysmall
sammysmall
+1  A: 

No - its creating an alias for the table

  • so there's less to type each time
  • and to deal with the scenario where you want to use multiple 'copies' of the table in the same query.
symcbean
Thank you for your input, it is appreciated!sammysmall
sammysmall
+1  A: 

cust is an alias for customer to make it shorter to type and read.

ho1
Thank you for your input it is appreciated!sammysmall
sammysmall
+1  A: 

'cust' is just an alias for the 'customer' table name. It allows you to write 'cust.name' rather than 'customer.name'

Stephen P
Thank you for your reply, it is appreciated!sammysmall
sammysmall
+1  A: 

You should have a look at Sql Server TABLE ALIASING

Have a closer look at SELECT Clause (Transact-SQL) and search for alias

astander
Thank you for the link as this will help me to locate the resource in a more direct manner!sammysmall
sammysmall
+5  A: 

cust is an alias for the Customer table. So instead of writing customer.custId, you can write cust.custId

Chris
+1 Concise, to the point, and includes an example.
Justin Niessner
And defeats the purpose of **HOMEWORK**
astander
I'm vehemently against do-my-homework questions, but in this case the homework is to "Describe the output of the following SQL query". If he had posed **that** as the question I would have downvoted him.
Stephen P
Yes I understand the homework aspect is critical, just a tad lost on this and had not seen this alias example yet... there are many things about this work that are not explained (even discussed) in a lecture... Thank you for keeping me on track!sammysmall
sammysmall
+1  A: 

cust is an alias for the customer table.

Since the query is looking at one table as two separate tables (or at least result sets) for the purposes of the query, this allows the DB to know that when you say "Customers" as the table name, you mean the ones from New York, while "Cust" means ones where the sales employee name is Daniel.

Alias names are described here: http://www.w3schools.com/sql/sql_alias.asp

David Stratton
Thank you for your answer, it is appreciated will keep working and learning!sammysmall
sammysmall