Using Except Clause in Postgresql

Using EXCEPT clause in PostgreSQL

Your query seems perfectly valid:

SELECT fk_id_tbl2 AS some_name
FROM table1
EXCEPT -- you may want to use EXCEPT ALL
SELECT pk_id
FROM table2;

Column names are irrelevant to the query. Only data types must match. The output column name of your query is fk_id_tbl2, just because it's the column name in the first SELECT. You can use any alias.

What's often overlooked: the subtle differences between EXCEPT (which folds duplicates) and EXCEPT ALL - which keeps all individual unmatched rows.
More explanation and other ways to do the same, some of them much more flexible:

  • Select rows which are not present in other table

Details for EXCEPT in the manual.

Select with except in postgresql

Use a VALUES list.

select * from (values (1),(2),(100000001)) as f (aid)
except
select aid from pgbench_accounts

How to emulate Postgresql EXCEPT operator in rails scope?

SELECT * FROM students
EXCEPT
SELECT * FROM students WHERE val->>'rating' = 'Fail'

equals to

Student.where("val->>'rating' IS DISTINCT FROM ?", 'Fail')

Using SQL EXCEPT just by primary key

Use NOT EXISTS operator and correlated subquery:

SELECT ckA1, ckA2, ... ckA9, columnA1, columnA2, ... columnAN
FROM A
WHERE NOT EXISTS (
SELECT 1 FROM B
WHERE ckA1 = ckB1, ckA2 = ckB2, ... ckA9 = ckB9
)

Postgres - select rows except where count is more than one and another condition

As it was suggested in the comment, you can use not exists:

select * 
from my_table mt
where mt.appuser_id = 6
and mt.role_id = 'BUSINESS_MANAGER'
and mt.business_id not in (
select imt.business_id
from my_table imt
where exists (
select *
from my_table iimt
where imt.appuser_id <> iimt.appuser_id
and iimt.appuser_id = mt.appuser_id
and iimt.role_id = 'CLIENT_ADMIN'
)
)

SQL Except Select with different number of columns in two tables

Use not exists as below

SELECT val_one, val_two, bool_val FROM normal n
where not exists
(
SELECT 1 FROM temp t where t.val_one=n.val_one and t.val_two=n.val_two
)

DB-Fiddle

 create table normal(val_one int, val_two int, bool_val varchar(10));
insert into normal values(1, 2, False);
insert into normal values(3, 4, False);

create table temp(val_one int, val_two int, bool_val varchar(10));
insert into temp values(1, 2, True);

Query:

 SELECT val_one, val_two, bool_val FROM normal n
where not exists
(
SELECT 1 FROM temp t where t.val_one=n.val_one and t.val_two=n.val_two
)

Output:

















val_oneval_twobool_val
34false

How to use DELETE with EXCEPT clause?

The reason it isn't working is that you're actually running two statements sequentially. Think of your code more like:

DECLARE @ClientID varchar = 'ClientA'

DELETE FROM Global.dto.ClientUsers;

SELECT ClientID, UserID FROM Global.dto.ClientUsers WHERE ClientID=@ClientID
EXCEPT
SELECT ClientID=@ClientID, UserID FROM ClientA_DB.dbo.Users;

If you want to modify the Delete statement, you need to follow it with a Where, Join, etc.

For some alternative methods to get the result you want, see the excellent answers at:
Using T-SQL EXCEPT with DELETE / Optimizing a query

PostgreSQL: NOT IN versus EXCEPT performance difference (edited #2)

Since you are running with the default configuration, try bumping up work_mem. Most likely, the subquery ends up getting spooled to disk because you only allow for 1Mb of work memory. Try 10 or 20mb.

How to use Except clause in Bigquery?

Try this instead:

select * EXCEPT (person_id) from 
person a
inner join hospital b
using (hosp_id)
inner join reading c
using (hosp_id)

You can only put column names (not paths) in the EXCEPT list, and you can simply avoid projecting the duplicate columns with USING instead of ON.



Related Topics



Leave a reply



Submit