Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8423903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:51:13+00:00 2026-06-10T03:51:13+00:00

If I create an alias in the select clause then I cannot use it

  • 0

If I create an alias in the select clause then I cannot use it in the where clause because according to the order of execution of sql queries where comes before select.

But I can create an alias in the select clause and use it in a having clause though having comes before select.

Why is it so?

Ex:

select type, (case when number>25 then 1 else 0 end) inc 
from animals
where inc='1';

this wont work. But,

select type, (case when number>25 then 1 else 0 end) inc 
from animals
having inc='1'; 

This works. Why so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:51:14+00:00Added an answer on June 10, 2026 at 3:51 am

    Basically because they where defined for different purposes. The WHERE clause is for records filtering and the HAVING clause is designed for filtering with aggregate functions (GROUP BY).
    In your second query an implicit GROUP BY filtering is being used, so for instance, if you add another column to the SELECT clause you will end up with different results.

    EDIT based on correction by Martin Smith

    HAVING was created to allow filtering of rows resulting of a GROUP BY. When no GROUP BY is specified, the whole result is considered a group.

    If neither a <where clause> nor a <group by clause> is specified,
    then let T be the result of the preceding <from clause>

    or

    …the group is the
    entire table if no <group by clause> is specified

    EDIT 2
    Now regarding the ALIAS:

    The specification for the WHERE clause regarding the columns references in the search condition says this:

    Each <column reference> directly contained in the <search
    condition>
    shall
    unambiguously reference a column of T or be an outer reference.

    Refer to: 7.6 <where clause>, Syntax Rule 1.

    The specification for the HAVING clause regarding the columns references in the search condition says this:

    Each <column reference> directly contained in the <search
    condition>
    shall unambiguously reference a grouping column of T
    or be an outer reference.

    Refer to: 7.8 <having clause>, Syntax Rule 1.

    And a grouping column is defined as:

    A column referenced in a <group by clause> is a grouping column.

    So in conclusion the WHERE must reference a column of the table and the HAVING clause must reference a grouping column of the group of rows.

    (Second Informal Review Draft) ISO/IEC 9075:1992, Database
    Language SQL- July 30, 1992

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to use AliasMatch to create an alias for everything within a folder,
I use the SQL Management Studio for create database I create a table and
In MySQL, it is possible to use an alias in GROUP BY clause that
When the type name is too long, in C# i can create alias like
I want to create a alias method in ruby and pass parameters to this.
I am trying to create a Finder Alias from the command line or from
Is there a way to create a shorter alias for an EditorAttribute? Instead of:
CREATE TABLE #TEMPTABLE ( requestTime datetime2, NoOfSessions integer ) INSERT INTO #TEMPTABLE select convert(nvarchar(16),
given a table create table mymy(A int(2),B int(2)) is it possible to use a
I'm using LINQ to SQL and DataGridViews to create an editable user interface of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.