Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use "where 1=2" on a SQL CREATE TABLE syntax?

Tags:

sql

CREATE TABLE EMPL_DEMO AS SELECT * FROM employees WHERE 1=2; 

I read this statement somewhere on the internet but I couldn't understand the WHERE 1=2.

Will anyone please, explain this?

like image 362
Omid Shagiwal Avatar asked Aug 18 '17 04:08

Omid Shagiwal


People also ask

What does WHERE 1/2 mean in SQL?

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. If you did this: select * into Table2 from Table1. Table2 would be an exact duplicate of Table1 , including the data rows.

Why do we use WHERE clause in SQL?

The SQL WHERE clause is used to specify a condition while fetching the data from a single table or by joining with multiple tables. If the given condition is satisfied, then only it returns a specific value from the table. You should use the WHERE clause to filter the records and fetching only the necessary records.

What is the output of select * from table name WHERE 1 2?

It means that MySQL has identified that it will result in zero rows.

What is the meaning of WHERE 1 0 in SQL?

A query like this can be used to ping the database. The clause: WHERE 1=0. Ensures that non data is sent back, so no CPU charge, no Network traffic or other resource consumption. A query like that can test for: server availability.


1 Answers

This type of command is usually used to copy the structure of one table to another. In this case, EMPL_DEMO will have the same column structure of employees, except for the keys or constraints.

The 1=2 always evaluates to False which prevents you from copying any of the rows.

like image 167
DrZoo Avatar answered Oct 20 '22 12:10

DrZoo