learn SQL and operations in ms sql server 2008, ms sql server 2012, ms sql server 2014, ms sql server 2016 In SQL and condition provides a concatenation of multiple condition in where statement in sql code. By definition, it can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement. This sql server tutorial provides a clear picture on delete statement which we can execute all types of ms sql server versions. As sql coding standard, when combining these conditions, it is very important to use parentheses so that the database knows what order to evaluate each condition. Syntax: The MySQL syntax for the SQL AND condition and OR condition together is:
select statement / delete statement / update statement /insert statement WHERE condition1 AND condition2 Parameters or Arguments: condition1, condition2,... condition_n: The conditions for data filtering in our sql query. Note: The SQL AND conditions allow us to test multiple conditions. Parentheses are included to specify the order of operation. And operation output in sql: AND Operation Condition 2 Condition 2 TRUE FALSE Condition 1 TRUE TRUE FALSE Condition 1 FALSE FALSE FALSE
From the above table we are able to see if both (left and right condition between and condition) the conditions satisfied the criteria of true. Then, the result set will be true or else. The result from the and condition is false. Example sql code with SELECT Statement: And condition provides an optimistic fetching of data if and only if both the conditions satisfied or else it will end up with fail condition which indicates and condition won t work.
Sample SQL Code: DROP TABLE dbo.customers CREATE TABLE dbo.customers(customerid INT, city VARCHAR(100)) INSERT INTO dbo.customers VALUES(1,'New York') INSERT INTO dbo.customers VALUES(2,'washington') INSERT INTO dbo.customers VALUES(2,'Los Angeles') INSERT INTO dbo.customers VALUES(4,'Chicago') INSERT INTO dbo.customers VALUES(5,'Houston') SELECT customerid,city FROM Customers SELECT customerid,city FROM Customers WHERE customerid = 2 SELECT customerid,city FROM Customers WHERE customerid = 2 AND city='washington' Go
Sample Output:
and fetches the data from the sql table. and returns the data based on the condition customerid=2. and returns the data based on two condition customerid=2 and city= washington. Code Explanation: In case, if the condition is customerid=3 and city= washington there is no matching record. So, sql server will return empty rows.
Here dbo.customers specifies to drop the existing table in the database. This code is optional. Here in this example creating the table dbo.customers with 2 columns id and city. And customerid is int, city is specified in varchar(100). Here VALUES(1, 'washington'),values(2, 'Los Angeles') specifies the data to be inserted into Wiki_firstTable table. Here in this code we fetch data from the table customers. Here we are Fetching data from the table with a condition customerid=2. Here we are Fetching data from the table with two conditions using and operation Customerid=2 and city= washington. Example sql code with INSERT Statement: Below ms sql code provides us an in-depth usage of and condition in insert statement.
Sample SQL Code DROP TABLE Customers CREATE TABLE Customers(customerid INT, city VARCHAR(100)) INSERT INTO Customers VALUES(1,'New York') INSERT INTO Customers VALUES(2,'washington') INSERT INTO Customers VALUES(2,'Los Angeles') INSERT INTO Customers VALUES(4,'Chicago') INSERT INTO Customers VALUES(5,'Houston') CREATE TABLE Customers_2(customerid INT, city VARCHAR(100)) INSERT INTO Customers_2(customerid,city) ( SELECT customerid,city FROM Customers WHERE customerid = 2 AND city='washington' ) SELECT * FROM Customers_2
Code Explanation: Here we are Droping the existing table (if any). This code is optional using the drop query. Here we are Creating the table customers with 2 columns id and city uisng create query. Here we are Inserting data into customers table uisng insert query. Here we are Creating another table customers_2 with 2 columns id and city uisng create query.
Now, we are going to insert data through a subquery select condition. The data satisfying the below condition(record with customerid=2 and city= washington from customers table) needs to be taken and inserted into the new table customers_2 SELECT customerid,city FROM Customers WHERE customerid = 2 AND city='washington' Here we are Fetching data from the table customers_2 table. To check, what are the datas inserted into the new table.
Here we are Inserting statement by selective fetching of data and inserting is done through our sql client - ms sql server management studio. Here the Data is inserted into the new table. 1 record satisfies the condition and it s inserted into the database. Example - With UPDATE Statement Below sql code provides us the procedure to use and condition in update statement: Sample SQL Code DROP TABLE Customers CREATE TABLE Customers(customerid INT, city VARCHAR(100)) INSERT INTO Customers VALUES(1,'New York') INSERT INTO Customers VALUES(2,'washington') INSERT INTO Customers VALUES(2,'Los Angeles') INSERT INTO Customers VALUES(4,'Chicago') INSERT INTO Customers VALUES(5,'Houston') SELECT * FROM Customers UPDATE Customers SET customerid = 6 WHERE customerid = 2 AND city='washington' SELECT * FROM Customers
Here we are Droping the existing table (if any). This code is optional uisng drop query. Here the table customers with 2 columns id and city is created. Using this query we are Inserting data into customers table using insert query. Here we are Creating another table customers_2 with 2 columns id and city uing create query. Now, we are going to update data :
The data satisfying the below condition(record with customerid=2 and city= washington from customers table) needs to be taken and its updated with the new value customerid=6 UPDATE Customers SET customerid = 6 WHERE customerid = 2 AND city='washington' Here we are Selecting the data from the table customers table. This is to check, whether our update statement executed succefully on the satisfied data.
Here we are Updating the statement with necessary conditions provided uing the update query. (Update the table with customer id=2 and city= washington with the customerid=6) 2 will become 6 now. Here in this output before updation, the value is 2. After updation the value becomes 6. In case, if the and condition in the above statement is not satisfied and if there are no records with customerid=2 and city= washington. No updates will happen. Example - With DELETE Statement Below Sql code provides us the procedure to use and condition in delete statement:
Sample SQL Code (transact sql): DROP TABLE Customers CREATE TABLE Customers(customerid INT, city VARCHAR(100)) INSERT INTO Customers VALUES(1,'New York') INSERT INTO Customers VALUES(2,'washington') INSERT INTO Customers VALUES(2,'Los Angeles') INSERT INTO Customers VALUES(4,'Chicago') SELECT * FROM Customers DELETE FROM Customers WHERE customerid = 2 AND city='washington' SELECT * FROM Customers
Code Explanation: Here we are Droping the existing table (if any). This code is optional. Here we are Creating the table customers with 2 columns id and city. Here wea re Inserting the data into customers table. Here wea are Creating another table customers_2 with 2 columns id and city. Here we are Fetch data from the table to check the existing data before updating it.
Now, we are going to delete the data. The data satisfying the below condition(record with customerid=2 and city= washington from customers table) needs to be taken and its deleted from the table delete Customers SET customerid = 6 WHERE customerid = 2 AND city='washington' Selecting the data from the table customers table. This is to check, whether our delete statement executed succefully on the satisfied condition.
Here the delete statement ( transact sql ) with necessary conditions provided. (delete the table with customer id=2 and city= washington with the customerid=6) 2 will become 6 now. Here in this output before updating the query the value becomes to 2. Here in this output after updating the query value becomes to 6.
In case, if the and condition in the above statement is not satisfied and if there are no records with customerid=2 and city= washington. No updates will happen.