|sql-server · database. this code is optional. here in this example creating the table...

19
|SQL-Server Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved. 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:

Upload: others

Post on 10-Oct-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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:

Page 2: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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

select statement / delete statement /

update statement /insert statement

WHERE condition1

AND condition2

Page 3: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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.

Page 4: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Sample SQL Code:

DROP TABLE dbo.Customers

GO

CREATE TABLE dbo.Customers(customerid INT, city

VARCHAR(100))

GO

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')

GO

SELECT customerid,city FROM Customers

GO

SELECT customerid,city FROM Customers WHERE

customerid = 2

GO

SELECT customerid,city FROM Customers WHERE

customerid = 2 AND city='washington'

Go

Page 5: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Sample Output:

Page 6: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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.

Page 7: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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.

Page 8: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Sample SQL Code

DROP TABLE Customers

GO

CREATE TABLE Customers(customerid INT, city

VARCHAR(100))

GO

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')

GO

CREATE TABLE Customers_2(customerid INT, city

VARCHAR(100))

GO

INSERT INTO Customers_2(customerid,city)

(

SELECT customerid,city FROM Customers WHERE

customerid = 2 AND city='washington'

)

GO

SELECT * FROM Customers_2

Page 9: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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.

Page 10: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Now, we are going to insert data through a subquery select

condition.

Here we are Fetching data from the table customers_2 table. To

check, what are the datas inserted into the new table.

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'

Page 11: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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

GO

CREATE TABLE Customers(customerid INT, city

VARCHAR(100))

GO

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')

GO

SELECT * FROM Customers

GO

UPDATE Customers SET customerid = 6

WHERE customerid = 2 AND city='washington'

GO

SELECT * FROM Customers

Page 12: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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 :

Page 13: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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.

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'

Page 14: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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:

Page 15: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Sample SQL Code (transact sql):

DROP TABLE Customers

GO

CREATE TABLE Customers(customerid INT, city

VARCHAR(100))

GO

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')

GO

SELECT * FROM Customers

GO

DELETE FROM Customers

WHERE customerid = 2 AND city='washington'

GO

SELECT * FROM Customers

Page 16: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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.

Page 17: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Now, we are going to delete the data.

Selecting the data from the table customers table. This is to

check, whether our delete statement executed succefully on the

satisfied condition.

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'

Page 18: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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.

Page 19: |SQL-Server · 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)

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

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.