Detailed explanation of the usage of exists in sql statement


1. Grammatical description

exists:

The result returned by the subquery sql statement in the brackets is not empty (that is, the result returned by sql is true), and the condition that the result of the subquery is not empty is satisfied, and the main sql is executed, otherwise it is not executed.

not exists:

Contrary to exists, the return result of the subquery sql statement in the brackets is empty (that is, the result not returned by sql is true), the condition is true if the result of the subquery is empty, and the main slq is executed, otherwise it is not executed.
Summary: The exists and not exists statements emphasize whether to return a result set, and do not require to know what to return. The difference from in is that in can only return one field value, and exists allows multiple fields to be returned.

2. Common examples

Create sample data, as shown in the following code, table a and table b are in a one-to-many relationship. The following sql uses modified sample data.

create table a(
  id int,
  name varchar(10)
);
insert into a values(1,'data1');
insert into a values(2,'data2');
insert into a values(3,'data3');

create table b(
  id int,
  a_id int,
  name varchar(10)
);
insert into b values(1,1,'info1');
insert into b values(2,2,'info2');
insert into b values(3,2,'info3');

create table c(
  id int,
  name varchar(10),
  c_date TIMESTAMP
);
insert into c values(1,'c1','2023-02-21 17:01:00');
insert into c values(2,'c2','2023-02-21 17:02:00');
insert into c values(2,'c3','2023-02-21 17:03:00');

1. Query table a and there is data in table b

It is equivalent to the in operation in sql.

select * from a where exists (select 1 from b where a_id=a.id )

The above sql is equivalent to the following sql

select * from a where id in (select a_id from b)

2. Query table a but there is no data in table b

It is equivalent to the not in operation in sql.

select * from a where not exists (select 1 from b where a_id=a.id )

The above sql is equivalent to the following sql

select * from a where id not in (select a_id from b)

3. Query the latest record of time

The following sql queries the most recent record of c_date within the same id.

SELECT * FROM c t1 
   WHERE NOT EXISTS(select * from c where id = t1.id and c_date>t1.c_date)

Analysis: In the subquery, first look at the case of id = 1, only when t1.c_date takes the maximum value, no result is returned, because it is the NOT EXISTS keyword, so the Where condition is established, and the query result that meets the condition is returned

4.exists replaces distinct to remove duplicate data

For example the following sql

SELECT distinct a.id,a.name from a, b WHERE a.id=b.a_id;

Use exists to raise duplicates, equivalent to the above sql

select id,name from a where exists (select 1 from b where a_id=a.id );

Analysis: The RDBMS core module will return the result immediately after the condition of the subquery is met, so it comes with deduplication

Summarize

Word document download address: Detailed explanation of the usage of exists in sql statement

Guess you like

Origin blog.csdn.net/ma286388309/article/details/129279863