What is the wrong with this SQL query?

SELECT Id, to_date(OrderDate,’YYYY’) AS OrderYear
FROM Order
WHERE OrderYear >= 2015;

Answer:
In the above query, OrderYear is an alias for to_date(OrderDate,’YYYY’) in SELECT clause.

When we are using OrderYear in WHERE clause, it is not available there.

Following is correct query:

SELECT Id, YEAR(OrderDate) AS OrderYear
FROM Order
WHERE YEAR(OrderDate) >= 2015;

Read the full book at www.amazon.com
Posted in SQL, SQL Interview Questions

Leave a Reply

Your email address will not be published. Required fields are marked *

*