Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of 1=2 in a SQL query

Tags:

sql

Someone please explain the meaning of '1=2' in the below SQL query.

SELECT E.EmpID,
       E.EmpName,
       Country = CASE
                   WHEN T.Active = 'N'
                        AND 1 = 2 THEN 'Not Working Anymore'
                   ELSE C.Country_Name
                 END,
       T.Contract_No
FROM   Employees E (nolock)
       INNER JOIN Contract T
         ON T.Contract_No = E.Contract_No
       LEFT JOIN Country C (nolock)
         ON E.Country_ID = C.Country_ID 

thanks

EDIT:- Corrected the slight mistake existed in the example SQL query given by me. @ ALL :- The query mentioned here is an example version of a big working query on which I have to reoslve something. I have created a sample scenario of SQL query for the sake of simplicity of question.

like image 293
Kings Avatar asked Mar 01 '13 13:03

Kings


People also ask

What does the query select * from table WHERE 1/2 do?

The reason you put the WHERE 1=2 clause in that SELECT INTO query is to create a field-copy of the existing table with no data. Table2 would be an exact duplicate of Table1 , including the data rows.

What is the result of select 1/2 as result?

When 1/2 is calculated arithmetically, SQL Server does internally knows that the answer is 0.5. Later on when it has to display the answer, it will covert 0.5 to integer and result is displayed as a 0 (zero).

What is the meaning of Group BY 1 2?

Consider above queries: Group by 1 means to group by the first column and group by 1,2 means to group by the first and second column and group by 1,2,3 means to group by first second and third column.

What is %% in SQL query?

The SQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters.


1 Answers

There is a good use for this 1=2 part of the WHERE clause if you are creating a table from another, but you don't want to copy any rows. For example:

CREATE TABLE ABC_TEMP AS
    SELECT * FROM ABC WHERE 1=2;
like image 191
Ian_T Avatar answered Sep 30 '22 14:09

Ian_T