ritesh gyanchandani ritesh gyanchandani - 6 months ago 5x
SQL Question

Having query in SQL aliases

The following SQL statement selects all the orders from the customer with

("Around the Horn"). We use the
tables, and give them the table aliases of
, respectively. (Here we have used aliases to make the SQL shorter):

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;

Can you please explain this syntax considering I am complete beginner.


With the SELECT statement you tell which columns of the table(s) you need. If there are more tables with the same column names you have to specificy also the table name (tableName.columnName).

With the FROM statement you tell the tables from which you want to select the datas. In this query there are two tables separated from a comma, this is not a good idea, because there is the JOIN statement to merge two tables in the correct way.

Finally with the WHERE statement you can tell conditions to filter the data. In this query you want only the customers with name = 'Around the Horn' and you also tell to select only the rows with the same customer id in the customer and order tables. This last condition should be specified with a join condition in the FROM:

FROM Customer AS c JOIN Orders AS o ON c.CustomerID = o.CustomerID

Otherwise using the comma in the FROM it does the cartesian product between the two tables and then it select only the correct rows with the second WHERE condition.

I suggest you to read a sql guide for beginners