Created
August 7, 2024 16:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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