Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Pxtl/386bcd39c9fef8824866ba95d11203ba to your computer and use it in GitHub Desktop.
Save Pxtl/386bcd39c9fef8824866ba95d11203ba to your computer and use it in GitHub Desktop.
Script that works in MS SQL but not in POSTGRES, demonstrating different behavior of "select *" within an EXISTS clause.
-- The following code works in MS SQL 2019
-- But fails in Postgres 14.12 (PG 14 was released in 2021)
DROP TABLE IF EXISTS EMPLOYEE;
-- create
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL,
dept VARCHAR(50) NOT NULL
);
-- insert
INSERT INTO EMPLOYEE VALUES (0001, 'Clark', 'Sales');
INSERT INTO EMPLOYEE VALUES (0002, 'Dave', 'Accounting');
INSERT INTO EMPLOYEE VALUES (0003, 'Ava', 'Sales');
-- fetch
SELECT CASE
WHEN EXISTS (
SELECT * FROM EMPLOYEE GROUP BY dept HAVING Count(*) > 1
) THEN 'there exists a dept with multiple employees'
ELSE 'there are no depts with multiple employees'
END AS Result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment