AS statement - where do we really need it?

Both of the codes listed below give an alias to the column name.
So where do we really need the AS statement? Where is it required?

SELECT COUNT(*) 'User Count', 
 plans.description 'Plan', 
 plans.price 'Price Category'
FROM premium_users
JOIN plans
  ON premium_users.membership_plan_id = plans.id
GROUP BY 2;
SELECT COUNT(*) AS 'User Count', 
 plans.description AS 'Plan', 
 plans.price AS 'Price Category'
FROM premium_users
JOIN plans
  ON premium_users.membership_plan_id = plans.id
GROUP BY 2;

I’m not 100% certain, but in the majority of cases I think it’s optional.

For the sake of a few extra keystrokes, and the increased legibility of the SQL (imho) it provides, I’d opt to use it unless the DB you’re working with doesn’t like it.

1 Like