EXISTS condition in SQL Server
In SQL Server (Transact-SQL) condition EXISTS is correct to associate with the internal query (subquery). The condition is met if the internal query returns at least 1 row. This condition can be used in SELECT, INSERT, UPDATE or DELETE commands.
EXISTS clause syntax in SQL Server
WHERE EXISTS ( subquery);
Variable name or variable value
subquery
Subquery - internal query is a SELECT command. If this query returns at least 1 record in the result set, the EXISTS clause is evaluated as true and the EXISTS condition is met. If the internal query does not return any records, the EXISTS clause is evaluated as false and the EXISTS condition is not met.
Note
Use an inefficient EXISTS condition due to internal RE-RUN query (rerun) on each row in the table in the external query. There are more effective ways without using EXISTS conditions.
For example - with SELECT statement
The SELECT statement is used with EXISTS conditions as shown below.
SELECT *
FROM nhanvien
WHERE EXISTS (SELECT *
FROM danhba
WHERE nhanvien.ho = danhba.ho
AND nhanvien.t
en = danhba.ten);
This example will return all records from the table of names when there is at least one table in the list of names that has the first and last names in the table.
For example - SELECT command uses NOT EXISTS
The EXISTS condition can be combined with the NOT operator.
SELECT *
FROM nhanvien
WHERE NOT EXISTS (SELECT *
FROM danhba
WHERE nhanvien.ho = danhba.ho
AND nhanvien
.ten = danhba.ten);
The returned result is all the records in the table of names if there is no record of family names and names in the list that match the first and last names in the table.
Example - INSERT command
This is an example of using the INSERT command with the EXISTS condition
INSERT INTOdanhba
(danhba_id, danhba_ten)
SELECT nhacung_id, nhacung_ten
FROM nhacung
WHERE EXISTS (SELECT *
FROM donhang
WHERE nhacung.n
hacung_id = donhang.nhacung_id);
Example - UPDATE command
Below is an example of an UPDATE statement using the EXISTS condition.
UPDATE ofacung
SET nhacung_ten = (SELECT khachhang.ten
FROM khachhang
WHERE khachhang.khachhang_id = nhacung.nhacung_id)
WHERE EXISTS (SELECT khachhang.ten
FROM khachhang
WHERE khachhan
g.khachhang_id = nhacung.nhacung_id);
Example - DELETE command
The DELETE command can also be used with EXISTS conditions as shown below.
DELETE FROM danhba
WHERE EXITS (SELECT *
FROM nhanvien
WHERE nhanvi
en.ho = danhba.ho);
Last lesson: TRUNCATE TABLE command in SQL Server
The following article: GROUP BY clause in SQL Server
You should read it
May be interested
- IN conditions in SQL Serverthe in condition is used in sql server (transact-sql) to minimize the need to use too many or conditions.
- How to Install, Configure, and Test Windows Server 2012 R2 Single Subnet DHCP Serverdynamic host configuration protocol (dhcp) offers several benefits for managing network properties, such as assigning an ip address to a dhcp client, and when the client no longer exists on the network, the dhcp server may assign the...
- How to fix printer name already exists error on Windows 11when installing a printer on windows 11, if the system detects that the printer name already exists, we will receive an error message. below are instructions to fix the error that the printer name already exists on windows 11.
- The difference between web server and app serveryou have probably seen that the terms web server and app server are often used interchangeably as if they are related to the same thing and also facilitate the website to function properly. but in reality, they are not the same.
- How to check if Email exists or not using command lineif you are experiencing email not being sent successfully, here are some tools to help you check if the recipient email address exists.
- BREAK (Control Interrupt) command in SQL Serverthe break command used to exit the loop does not specify a stop condition or you want to stop the loop on condition that you specify and execute the statements following the loop statement end.
- Network basics: Part 3 - DNS Servera dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
- If ... Else in Cwhat is the if … else condition in c? what is the meaning and usage of if else statement in c? let's find out with tipsmake.com.com!
- How to set up your own Git server on Linuxwhile you can count on globally renowned git hosting services like github, in some cases it is better to host a personal git server for enhanced privacy, customizability, and security.
- Google Lens has been updated with skin condition detection featuresgoogle lens has introduced a new feature that can help identify certain health conditions. now, by pointing the smartphone camera at a specific area of skin that is prone to irritation or rashes.