88
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?

4
  • 4
    tl;dr 1=2 is evaluated as FALSE for all rows Aug 18, 2017 at 8:33
  • 31
    It's a little hack to avoid having to type the entire column structure again. It will create a table with the same column names and data types, but without copying any data rows or keys.
    – Adwaenyth
    Aug 18, 2017 at 8:45
  • this save 2 (two) char when typed "1=2" : 3 char, "False" : 5 char
    – Archemar
    Aug 20, 2017 at 15:07
  • 1
    Please note that this is by no means standard SQL. It works in MySQL, not sure about other dialects. Aug 23, 2017 at 14:59

7 Answers 7

113

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.

7
  • 2
    @jpmc26 I guess "identical" was a poor choice. I was just referring to the column structure.
    – DrZoo
    Aug 18, 2017 at 14:20
  • It will also innit any indexes or constraints. Essentially you'll make a copy of the columns with the appropriate data types
    – Barranka
    Aug 18, 2017 at 15:55
  • 7
    If you wanted to copy all the constraints, indexes, etc. as well, you would use CREATE TABLE new_table LIKE old_table
    – Barmar
    Aug 18, 2017 at 17:46
  • 1
    @Barmar unless it's something new, LIKE old_table is not a SQL Server construct...
    – WernerCD
    Aug 18, 2017 at 22:57
  • 1
    I think your meaning of column structure is just name and type of the column, Consider that there are some other properties also as column structure - e.g. default value, alias and etc. ;).
    – shA.t
    Aug 19, 2017 at 4:47
30

CREATE TABLE (Create A New Table)

EMPL_DEMO (Called EMPL_DEMO)

AS (With The Data and structure of)

SELECT * FROM employees WHERE 1=2; (Everything in employees where 1=2. Since 1 is never 2 - copy the structure and all 0 matching rows)

..Essentially copy structure and not data.

15

This syntax does the same, but it's more obvious, it creates a table with the same structure, with no data.

CREATE TABLE EMPL_DEMO AS SELECT * FROM employees limit 0;
4
  • 2
    LIMIT has some popularity, but it is nowhere close to a standard (unfortunately)
    – bgusach
    Aug 22, 2017 at 20:40
  • 1
    @bgusach, jeeze, that is so crazy. I couldn't imagine that any realistic DB could possibly not have LIMIT. But it seems MSSQL lacks it (which strikes me as a good reason to just not use MSSQL). Apparently they have TOP, but google results have so many people not using that, so there's probably some hijink surrounding it.
    – Kat
    Aug 22, 2017 at 21:50
  • @Kat Some have it, many others don't (including standard SQL). What is it so special about this specific syntax? Aug 23, 2017 at 14:57
  • It's not about the syntax, @ÁlvaroGonzález, but the functionality itself is pretty critical IMO. Maybe not so much for this case (although I see it more readable than the other options), but it's obviously useful for pagination of results. When I looked up "MSSQL LIMIT", some of the workarounds I saw suggested were crazy for a feature that feels like an integral part of SQL (I was also surprised that it wasn't standard -- who can keep track of what is and isn't with SQL so fragmented?).
    – Kat
    Aug 23, 2017 at 16:31
7

This can be useful to copy structure of a table excluding its constraints, keys, indexes, identity property and data rows.

This query will create EMPL_DEMO table with no rows copied from employees table as WHERE 1=2 condition is always going to be evaluated as FALSE.

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

Trust in Google, my friend. From the bottom of the first result when you google that statement:

For example:

CREATE TABLE suppliers
  AS (SELECT *
    FROM companies WHERE 1=2);

This would create a new table called suppliers that included all columns from the companies table, but no data from the companies table.

5
  • 42
    Note that Google search results are customized for each user, based on whatever Google happens to know about the user. So the ordering of results is likely to be different for everyone, and consequently, "the first result" risks being different for everyone. In my case, the top result for that query is this question, and the page you link to doesn't even show up among the three hits.
    – user
    Aug 18, 2017 at 10:53
  • 2
    When referencing the nth result, use a search engine like DuckDuckGo that has static results over time for each query.
    – wizzwizz4
    Aug 19, 2017 at 20:12
  • @MichaelKjörling There used to be an option to show static ("unpersonalized") results. I don't know how well it worked when it was around, but it's gone now, since it seems that user data has become so intertwined with the PageRank algorithm.
    – oldmud0
    Aug 19, 2017 at 23:50
  • 1
    @oldmud0: I don't think it's gone? Between Private Results and Remove all custom results I think it should be configurable?
    – user541686
    Aug 20, 2017 at 11:09
  • 1
    @Mehrdad The second link says under "Remove all custom results" that one of the required steps is to sign in to your Google account, so that is obviously not available to those who do not have a Google account. Also, the name of the setting indicates that it only affects ads, not search results, but the text does hint at that it also affects results, so that is unclear at best.
    – user
    Aug 20, 2017 at 12:05
3

In SQL Server

select * into table1 from table2 where 1=2(Only Structure)

select * into table1 from table2 where 1=1(Structure with data)

1

Oracle:

CREATE TABLE EMPL_DEMO AS SELECT * FROM employees WHERE 1=2; //just structure not data

CREATE TABLE EMPL_DEMO AS SELECT * FROM employees WHERE 1=1; //ststructure and data

Better understanding in the Teradata Database:

CREATE TABLE EMPL_DEMO AS Memployees with no data; //structure

CREATE TABLE EMPL_DEMO AS Memployees with data; //structure and data

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.