Tutorials SQL Server by Amit Sir
Tutorials SQL Server by Amit Sir
DBMS TUTORIAL
Description
The SQL Server (Transact-SQL) SELECT statement is used to retrieve records from one or more tables in a SQL Server database.
Syntax
In its simplest form, the syntax for the SELECT statement in SQL Server (Transact-SQL) is:
SELECT expressions
FROM tables
[WHERE conditions];
However, the full syntax for the SELECT statement in SQL Server (Transact-SQL) is:
Parameters or Arguments
ALL
Optional. Returns all matching rows.
DISTINCT
Optional. Removes duplicates from the result set. Learn more about the DISTINCT clause
TOP (top_value)
Optional. If specified, it will return the top number of rows in the result set based on top_value. For example, TOP(10)
would return the top 10 rows from the full result set.
PERCENT
Optional. If specified, then the top rows are based on a percentage of the total result set (as specfied by the top_value).
For example, TOP(10) PERCENT would return the top 10% of the full result set.
WITH TIES
Optional. If specified, then rows tied in last place within the limited result set are returned. This may result in more rows be
returned than the TOP parameter permits.
expressions
The columns or calculations that you wish to retrieve. Use * if you wish to select all columns.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
WHERE conditions
Optional. The conditions that must be met for the records to be selected.
GROUP BY expressions
Optional. It collects data across multiple records and groups the results by one or more columns.
HAVING condition
Optional. It is used in combination with the GROUP BY to restrict the groups of returned rows to only those whose the
condition is TRUE.
ORDER BY expression
Compiled By : Amit Srivastava
Optional. It is used to sort the records in your result set. ASC sorts in ascending order and DESC sorts in descending
order.
SELECT *
FROM inventory
WHERE quantity > 5
ORDER BY inventory_id ASC;
In this SQL Server SELECT statement example, we've used * to signify that we wish to select all fields from the inventory table
where the quantity is greater than 5. The result set is sorted by inventory_id in ascending order.
This SQL Server SELECT example would return only the inventory_id, inventory_type, and quantity fields from the inventory table
where the inventory_id is greater than or equal to 555 and the inventory_type is 'Software'. The results are sorted by quantity in
descending order and then inventory_id in ascending order.
This SQL Server SELECT example joins two tables together to gives us a result set that displays the inventory_id, product_name,
and quantity fields where the product_id value matches in both the inventory and products table. The results are sorted
by inventory_id in ascending order.
SELECT TOP(3)
inventory_id, inventory_type, quantity
Compiled By : Amit Srivastava
FROM inventory
WHERE inventory_type = 'Software'
ORDER BY inventory_id ASC;
This SQL Server SELECT example would select the first 3 records from the inventory table where the inventory_type is 'Software'. If
there are other records in the inventory table that have a inventory_type value of 'Software', they will not be returned by the SELECT
statement.
This SQL Server SELECT example would select the first 10% of the records from the full result set. So in this example, the SELECT
statement would return the top 10% of records from the inventory table where the inventory_type is 'Software'. The other 90% of the
result set would not be returned by the SELECT statement.
Description
The SQL Server (Transact-SQL) FROM clause is used to list the tables and any joins required for the query in SQL Server.
Syntax
The syntax for the FROM clause in SQL Server (Transact-SQL) is:
FROM table1
[ { INNER JOIN
| LEFT OUTER JOIN
| RIGHT OUTER JOIN
| FULL OUTER JOIN } table2
ON table1.column1 = table2.column1 ]
Parameters or Arguments
table1 and table2
The tables used in the SQL statement. The two tables are joined based on table1.column1 = table2.column1.
Note
• There must be at least one table listed in the FROM clause in SQL Server (Transact-SQL).
• If there are two or more tables listed in the FROM clause, these tables are generally joined in the FROM clause using
INNER or OUTER joins. Although the tables can also be joined using the old syntax in the WHERE clause, we
recommend using new standards and including your join information in the FROM clause.
Compiled By : Amit Srivastava
Example - With one table
It is difficult to explain the syntax for the SQL Server FROM clause, so let's look at some examples.
We'll start by looking at how to use the FROM clause with only a single table.
For example:
SELECT *
FROM employees
WHERE first_name = 'Jane';
In this SQL Server FROM clause example, we've used the FROM clause to list the table called employees. There are no joins
performed since we are only using one table.
Description
Comparison operators are used in the WHERE clause to determine which records to select. Here is a list of the comparison
operators that you can use in SQL Server (Transact-SQL):
= Equal
!= Not Equal
SELECT *
FROM employees
WHERE first_name = 'Jane';
In this example, the SELECT statement above would return all rows from the employees table where the first_name is equal to
Jane.
SELECT *
FROM employees
WHERE first_name <> 'Jane';
In this example, the SELECT statement would return all rows from the employees table where the first_name is not equal to Jane.
Or you could also write this query using the != operator, as follows:
SELECT *
FROM employees
WHERE first_name != 'Jane';
SELECT *
FROM employees
WHERE employee_id > 3000;
In this example, the SELECT statement would return all rows from the employees table where the employee_id is greater than
3000. An employee_id equal to 3000 would not be included in the result set.
SELECT *
FROM employees
WHERE employee_id >= 3000;
In this example, the SELECT statement would return all rows from the employees table where the employee_id is greater than or
equal to 3000. In this case, n employee_id equal to 3000 would be included in the result set.
SELECT *
FROM employees
WHERE employee_id < 500;
In this example, the SELECT statement would return all rows from the employees table where the employee_id is less than 500.
An employee_id equal to 500 would not be included in the result set.
SELECT *
FROM employees
WHERE employee_id <= 500;
In this example, the SELECT statement would return all rows from the employees table where the employee_id is less than or equal
to 500. In this case, n employee_id equal to 500 would be included in the result set.
Description
The SQL Server (Transact-SQL) IN condition is used to help reduce the need to use multiple OR conditions in a SELECT, INSERT,
UPDATE, or DELETE statement.
Syntax
The syntax for the IN condition in SQL Server (Transact-SQL) is:
OR
expression IN (subquery);
Parameters or Arguments
expression
Compiled By : Amit Srivastava
A value to test.
value1, value2,.. value_n
The values to test against expression.
subquery
This is a SELECT statement whose result set will be tested against expression. If any of these values
matches expression, then the IN condition will evaluate to true.
Note
• The SQL Server IN condition will return the records where expression is value1, value2..., or value_n.
• The SQL Server IN condition is also called the SQL Server IN operator.
SELECT *
FROM employees
WHERE last_name IN ('Smith', 'Anderson', 'Johnson');
This SQL Server IN condition example would return all rows from the employees table where the last_name is either 'Smith',
'Anderson', or 'Johnson'. Because the * is used in the SELECT, all fields from the employees table would appear in the result set.
The above IN example is equivalent to the following SELECT statement:
SELECT *
FROM employees
WHERE last_name = 'Smith'
OR last_name = 'Anderson'
OR last_name = 'Johnson';
As you can see, using the SQL Server IN condition makes the statement easier to read and more efficient.
SELECT *
FROM employees
WHERE employee_id IN (1, 2, 3, 4, 10);
This SQL Server IN condition example would return all employees where the employee_id is either 1, 2, 3, 4, or 10.
The above IN example is equivalent to the following SELECT statement:
SELECT *
FROM employees
WHERE employee_id = 1
OR employee_id = 2
OR employee_id = 3
OR employee_id = 4
OR employee_id = 10;
Compiled By : Amit Srivastava
SELECT *
FROM employees
WHERE first_name NOT IN ('Sarah', 'John', 'Dale');
This SQL Server IN condition example would return all rows from the employees table where the first_name is not 'Sarah', 'John', or
'Dale' Sometimes, it is more efficient to list the values that you do not want, as opposed to the values that you do want.
The above IN example is equivalent to the following SELECT statement:
SELECT *
FROM employees
WHERE first_name <> 'Sarah'
AND first_name <> 'John'
AND first_name <> 'Dale';
Description
The SQL Server (Transact-SQL) NOT condition, also called the NOT Operator, is used to negate a condition in a SELECT, INSERT,
UPDATE, or DELETE statement.
Syntax
The syntax for the NOT condition in SQL Server (Transact-SQL) is:
NOT condition
Parameters or Arguments
condition
The condition to negate.
Note
• The SQL Server NOT condition requires that the opposite of the condition must be met for the record to be included in the
result set.
SELECT *
FROM employees
Compiled By : Amit Srivastava
WHERE first_name NOT IN ( 'John', 'Dale', 'Susan' );
This SQL Server NOT example would return all rows from the employees table where the first_name is not 'John', 'Dale', or 'Susan'.
Sometimes, it is more efficient to list the values that you do not want, as opposed to the values that you do want.
SELECT *
FROM employees
WHERE last_name IS NOT NULL;
This SQL Server NOT example would return all records from the employees table where the last_name does not contain a NULL
value.
By placing the SQL Server NOT Operator in front of the LIKE condition, you are able to retrieve all employees
whose last_name does not start with 'A'.
SELECT *
FROM employees
WHERE employee_id NOT BETWEEN 200 AND 250;
This SQL Server NOT example would return all rows from the employees table where the employee_id was NOT between 200 and
250, inclusive. It would be equivalent to the following SQL Server SELECT statement:
SELECT *
FROM employees
WHERE employee_id < 200
OR employee_id > 250;
SELECT *
FROM employees
Compiled By : Amit Srivastava
WHERE NOT EXISTS (SELECT *
FROM contacts
WHERE employees.last_name = contacts.last_name
AND employees.first_name = contacts.first_name);
This SQL Server NOT example would return all records from the employees table where there are no records in the contacts table
for the matching last_name and first_name.
Description
The SQL Server (Transact-SQL) BETWEEN condition is used to retrieve values within a range in a SELECT, INSERT, UPDATE, or
DELETE statement.
Syntax
The syntax for the BETWEEN condition in SQL Server (Transact-SQL) is:
Parameters or Arguments
expression
A column or calculation.
value1 and value2
These values create an inclusive range that expression is compared to.
Note
• The SQL Server BETWEEN condition will return the records where expression is within the range
of value1 and value2 (inclusive).
SELECT *
FROM employees
WHERE employee_id BETWEEN 25 AND 100;
This SQL Server BETWEEN example would return all rows from the employees table where the employee_id is between 25 and
100 (inclusive). It is equivalent to the following SELECT statement:
SELECT *
FROM employees
WHERE employee_id >= 25
AND employee_id <= 100;
Compiled By : Amit Srivastava
SELECT *
FROM employees
WHERE start_date BETWEEN '2014/05/01' AND '2014/05/31';
This SQL Server BETWEEN condition example would return all records from the employees table where the start_date is between
May 1, 2014 and May 31, 2014 (inclusive). It would be equivalent to the following SELECT statement:
SELECT *
FROM employees
WHERE start_date >= '2014/05/01'
AND start_date <= '2014/05/31';
SELECT *
FROM employees
WHERE employee_id NOT BETWEEN 2000 AND 2999;
This SQL Server BETWEEN example would return all rows from the employees table where the employee_id was NOT between
2000 and 2999, inclusive. It would be equivalent to the following SELECT statement:
SELECT *
FROM employees
WHERE employee_id < 2000
OR employee_id > 2999;
Description
The SQL Server (Transact-SQL) IS NULL condition is used to test for a NULL value.
Syntax
The syntax for the IS NULL condition in SQL Server (Transact-SQL) is:
expression IS NULL
Parameters or Arguments
Compiled By : Amit Srivastava
expression
The value to test whether it is a NULL value.
Note
SELECT *
FROM employees
WHERE last_name IS NULL;
This SQL Server IS NULL example will return all records from the employees table where the last_name contains a null value.
This SQL Server IS NULL example will insert records into the employees table records from the contacts table where
the first_name contains a null value.
UPDATE employees
SET first_name = 'Unknown'
WHERE first_name IS NULL;
This SQL Server IS NULL example will update records in the employees table where the first_name contains a null value.
Description
The SQL Server (Transact-SQL) IS NOT NULL condition is used to test for a NOT NULL value.
Syntax
The syntax for the IS NOT NULL condition in SQL Server (Transact-SQL) is:
Parameters or Arguments
expression
The value to test where it is a non-NULL value.
Note
SELECT *
FROM employees
WHERE last_name IS NOT NULL;
This SQL Server IS NOT NULL example will return all records from the employees table where the last_name does not contain a
null value.
This SQL Server IS NOT NULL example will insert records into the contacts table where the last_name does not contain a null value
in the employees table.
Compiled By : Amit Srivastava
UPDATE employees
SET status = 'Active'
WHERE last_name IS NOT NULL;
This SQL Server IS NOT NULL example will update records in the employees table where the last_name does not contain a null
value.
This SQL Server IS NOT NULL example will delete all records from the employees table where the status does not contain a null
value.
Description
The SQL Server (Transact-SQL) LIKE condition allows wildcards to be used in the WHERE clause of a SELECT, INSERT,
UPDATE, or DELETE statement. This allows you to perform pattern matching.
Syntax
The syntax for the LIKE condition in SQL Server (Transact-SQL) is:
Parameters or Arguments
expression
A character expression such as a column or field.
pattern
A character expression that contains pattern matching. The patterns that you can choose from are:
Wildcard Explanation
% Allows you to match any string of any length (including zero length)
[] Allows you to match on any character in the [ ] brackets (for example, [abc]
Compiled By : Amit Srivastava
Wildcard Explanation
Allows you to match on any character not in the [^] brackets (for example,
[^]
[^abc] would match on any character that is not a, b, or c characters)
escape_character
Optional. It allows you to test for literal instances of a wildcard character such as % or _.
SELECT *
FROM employees
WHERE last_name LIKE 'B%';
You can also using the % wildcard multiple times within the same string. For example,
SELECT *
FROM employees
WHERE last_name LIKE '%o%';
In this SQL Server LIKE condition example, we are looking for all employees whose last_name contains the letter 'o'.
SELECT *
FROM employees
WHERE first_name LIKE 'Ad_m';
This SQL Server LIKE condition example would return all employees whose first_name is 4 characters long, where the first two
characters is 'Ad' and the last character is 'm'. For example, it could return employees whose first_name is 'Adam', 'Adem', 'Adim',
'Adom', 'Adum', etc.
Here is another example:
SELECT *
FROM employees
WHERE employee_number LIKE '123_';
You might find that you are looking for an employee_number, but you only have 3 of the 4 digits. The example above, would retrieve
potentially 10 records back (where the missing value could equal anything from 0 to 9). For example, it could return employees
whose employee numbers are:
1230, 1231, 1232, 1233, 1234, 1235, 1236, 1237, 1238, 1239
Compiled By : Amit Srivastava
SELECT *
FROM employees
WHERE first_name LIKE 'Sm[iy]th';
This SQL Server LIKE condition example would return all employees whose first_name is 5 characters long, where the first two
characters is 'Sm' and the last two characters is 'th', and the third character is either 'i' or 'y'. So in this case, it would match on either
'Smith' or 'Smyth'.
SELECT *
FROM employees
WHERE first_name LIKE 'Sm[^iy]th';
This SQL Server LIKE condition example would return all employees whose first_name is 5 characters long, where the first two
characters is 'Sm' and the last two characters is 'th', and the third character is neither 'i' or 'y'. So in this case, it would match on
values such as 'Smath', 'Smeth', 'Smoth', etc. But it would not match on either 'Smith' or 'Smyth'.
SELECT *
FROM employees
WHERE last_name NOT LIKE 'B%';
By placing the NOT Operator in front of the SQL Server LIKE condition, you are able to retrieve all employees
whose last_name does not start with 'B'.
SELECT *
FROM employees
WHERE secret_hint LIKE '123!%455' ESCAPE '!';
Compiled By : Amit Srivastava
This SQL Server LIKE condition example identifies the ! character as an escape character. This statement will return all employees
whose secret_hint is 123%455.
Here is another more complicated example using escape characters in the SQL Server LIKE condition.
SELECT *
FROM employees
WHERE secret_hint LIKE 'H%!%' ESCAPE '!';
This SQL Server LIKE condition example returns all employees whose secret_hint starts with H and ends in %. For example, it
would return a value such as 'Help%'.
You can also use the escape character with the _ character in the SQL Server LIKE condition.
For example:
SELECT *
FROM employees
WHERE secret_hint LIKE 'H%!_' ESCAPE '!';
This SQL Server LIKE condition example returns all employees whose secret_hint starts with H and ends in _. For example, it would
return a value such as 'Help_'.
Syntax
The syntax for the EXISTS condition in SQL Server (Transact-SQL) is:
Parameters or Arguments
subquery
The subquery is a SELECT statement. If the subquery returns at least one record in its result set, the EXISTS clause will
evaluate to true and the EXISTS condition will be met. If the subquery does not return any records, the EXISTS clause will
evaluate to false and the EXISTS condition will not be met.
Note
• SQL statements that use the EXISTS condition are very inefficient since the sub-query is RE-RUN for EVERY row in the
outer query's table. There are more efficient ways to write most queries, that do not use the EXISTS condition.
SELECT *
FROM employees
WHERE EXISTS (SELECT *
Compiled By : Amit Srivastava
FROM contacts
WHERE employees.last_name = contacts.last_name
AND employees.first_name = contacts.first_name);
This SQL Server EXISTS condition example will return all records from the employees table where there is at least one record in
the contacts table with a matching last_name and first_name.
SELECT *
FROM employees
WHERE NOT EXISTS (SELECT *
FROM contacts
WHERE employees.last_name = contacts.last_name
AND employees.first_name = contacts.first_name);
This SQL Server EXISTS example will return all records from the employees table where there are no records in the contacts table
for the matching last_name and first_name.
UPDATE suppliers
SET supplier_name = (SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id)
WHERE EXISTS (SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id);
Description
The SQL Server (Transact-SQL) WHERE clause is used to filter the results from a SELECT, INSERT, UPDATE, or DELETE
statement.
Syntax
The syntax for the WHERE clause in SQL Server (Transact-SQL) is:
WHERE conditions;
Parameters or Arguments
conditions
The conditions that must be met for records to be selected.
SELECT *
FROM employees
WHERE first_name = 'Jane';
In this SQL Server WHERE clause example, we've used the WHERE clause to filter our results from the employees table. The
SELECT statement above would return all rows from the employees table where the first_name is 'Jane'. Because the * is used in
the SELECT, all fields from the employees table would appear in the result set.
SELECT *
FROM employees
WHERE last_name = 'Anderson'
AND employee_id >= 3000;
Compiled By : Amit Srivastava
This SQL Server WHERE clause example uses the WHERE clause to define multiple conditions. In this case, this SELECT
statement uses the AND condition to return all employees that have a last_name of 'Anderson' and the employee_id is greater than
or equal to 3000.
This SQL Server WHERE clause example uses the WHERE clause to define multiple conditions, but instead of using the AND
condition, it uses the OR condition. In this case, this SELECT statement would return all employee_id, last_name,
and first_name values from the employees table where the last_name is 'Johnson' or the first_name is 'Danielle'.
SELECT *
FROM employees
WHERE (state = 'California' AND last_name = 'Smith')
OR (employee_id = 82);
This SQL Server WHERE clause example uses the WHERE clause to define multiple conditions, but it combines the AND
condition and the OR condition. This example would return all employees that reside in the state of 'California' and
whose last_name is 'Smith' as well as all employees whose employee_id is equal to 82.
The parentheses determine the order that the AND and OR conditions are evaluated. Just like you learned in the order of operations
in Math class!
This SQL Server WHERE clause example uses the WHERE clause to join multiple tables together in a single SELECT statement.
This SELECT statement would return all rows where the first_name in the employees table is 'Sarah'. And the employees
and contacts tables are joined on the employee_id from the employees table and the contact_id from the contacts table.
Description
The SQL Server (Transact-SQL) ORDER BY clause is used to sort the records in your result set. The ORDER BY clause can only
be used in SELECT statements.
Syntax
The syntax for the ORDER BY clause in SQL Server (Transact-SQL) is:
SELECT expressions
FROM tables
[WHERE conditions]
ORDER BY expression [ ASC | DESC ];
Parameters or Arguments
expressions
The columns or calculations that you wish to retrieve.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
WHERE conditions
Optional. The conditions that must be met for the records to be selected.
ASC
Optional. It sorts the result set in ascending order by expression (default, if no modifier is provider).
DESC
Optional. It sorts the result set in descending order by expression.
Note
• If the ASC or DESC modifier is not provided in the ORDER BY clause, the results will be sorted by expression in
ascending order. This is equivalent to ORDER BY expression ASC.
SELECT last_name
FROM employees
WHERE employee_id > 1000
ORDER BY last_name;
This SQL Server ORDER BY example would return all records sorted by the last_name field in ascending order and would be
equivalent to the following ORDER BY clause:
SELECT last_name
FROM employees
WHERE employee_id > 1000
ORDER BY last_name ASC;
SELECT last_name
FROM employees
WHERE first_name = 'Sarah'
ORDER BY last_name DESC;
This SQL Server ORDER BY example would return all records sorted by the last_name field in descending order.
SELECT last_name
FROM employees
WHERE last_name = 'Anderson'
ORDER BY 1 DESC;
This SQL Server ORDER BY would return all records sorted by the last_name field in descending order, since the last_name field is
in position #1 in the result set and would be equivalent to the following ORDER BY clause:
SELECT last_name
FROM employees
WHERE last_name = 'Anderson'
ORDER BY last_name DESC;
This SQL Server ORDER BY would return all records sorted by the last_name field in descending order, with a secondary sort
by first_name in ascending order.
Description
The SQL Server (Transact-SQL) AND condition (also called the AND Operator) is used to test for two or more conditions in a
SELECT, INSERT, UPDATE, or DELETE statement.
Compiled By : Amit Srivastava
Syntax
The syntax for the AND condition in SQL Server (Transact-SQL) is:
WHERE condition1
AND condition2
...
AND condition_n;
Parameters or Arguments
condition1, condition2, ... condition_n
All of the conditions that must be met for the records to be selected.
Note
• The SQL Server AND condition allows you to test 2 or more conditions.
• The SQL Server AND condition requires that all of the conditions (ie: condition1, condition2, condition_n) must be met for
the record to be included in the result set.
SELECT *
FROM employees
WHERE last_name = 'Smith'
AND employee_id < 499;
This SQL Server AND example would return all employees who have a last_name of 'Smith' and have an employee_id less than
499. Because the * is used in the SELECT statement, all fields from the employees table would appear in the result set.
Though the above SQL works just fine, you would more traditionally write this SQL as follows using a proper INNER JOIN.
For example:
This SQL Server AND condition example would insert into the contacts table, all employee_id, last_name, and first_name records
from the employees table where the first_name is 'Joanne' and the employee_id is greater than or equal to 800.
UPDATE employees
SET last_name = 'Johnson'
WHERE last_name = 'TBD'
AND employee_id < 300;
This SQL Server AND condition example would update all last_name values in the employees table to 'Johnson' where
the last_name is 'TBD' and the employee_id is less than 300.
This SQL Server AND condition example would delete all records from the employees table whose first_name is 'Darlene'
and last_name is 'Henderson'.
Description
Compiled By : Amit Srivastava
The SQL Server (Transact-SQL) OR condition is used to test multiple conditions where records are returned when any one of the
conditions are met. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.
Syntax
The syntax for the OR condition in SQL Server (Transact-SQL) is:
WHERE condition1
OR condition2
...
OR condition_n;
Parameters or Arguments
condition1, condition2, ... condition_n
Any of the conditions that must be met for the records to be selected.
Note
SELECT *
FROM employees
WHERE first_name = 'Sarah'
OR last_name = 'Johnson';
This SQL Server OR example would return all employees whose first_name is 'Sarah' or last_name is 'Johnson'. Because the * is
used in the SELECT statement, all fields from the employees table would appear in the result set.
This SQL Server OR condition example would return all last_name and first_name values from the employees table where
the last_name is 'Anderson' or the state is 'California' or the employee_id is equal to 50.
This SQL Server OR example would insert into the contacts table, all employee_id, last_name, and first_name records from
the employees table where the last_name is 'Smith' or the employee_id is less than 10.
UPDATE employees
SET state = 'Florida'
WHERE employee_id < 1000
OR city = 'Miami';
This SQL Server OR condition example would update all state values in the employees table to 'Florida' where the employee_id is
less than 1000 or the city is 'Miami'.
This SQL Server OR condition example would delete all employees from the employees table whose first_name is either 'Joanne' or
'Darlene'.
Description
The SQL Server (Transact-SQL) AND condition and OR condition can be combined in a SELECT, INSERT, UPDATE, or DELETE
statement.
When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each
condition. (Just like when you were learning the order of operations in Math class!)
Syntax
The syntax for the AND condition and the OR condition together in SQL Server (Transact-SQL) is:
Compiled By : Amit Srivastava
WHERE condition1
AND condition2
...
OR condition_n;
Parameters or Arguments
condition1, condition2, ... condition_n
The conditions that are evaluated to determine if the records will be selected.
Note
• The SQL Server AND & OR conditions allow you to test multiple conditions.
• Don't forget the order of operation parentheses!
SELECT *
FROM employees
WHERE (last_name = 'Anderson' AND first_name = 'Sarah')
OR (employee_id = 75);
This AND & OR example would return all employees whose last_name is 'Anderson' and first_name is 'Sarah, as well as all
employees whose employee_id is 75. The parentheses determine the order that the AND and OR conditions are evaluated. Just like
you learned in the order of operations in Math class!
The next example takes a look at a more complex statement.
For example:
This AND & OR example would return all employee_id, last_name, and first_name values where the last_name is 'Smith' OR
the last_name is 'Anderson' and the first_name is 'Sarah' OR the employee_id is greater than 1000 and the state is 'California'.
This SQL Server AND and OR example would insert into the contacts table, all last_name and first_name values from
the employees table whose last_name is either 'Johnson' or 'Anderson' and where the employee_id is greater than 54.
UPDATE employees
SET last_name = 'TBD'
WHERE employee_id <= 2000
AND (state = 'California' OR state = 'Arizona');
This SQL Server AND & OR condition example would update all last_name values in the employees table to 'TBD' where
the employee_id is less than or equal to 2000 and resides in either the state of 'California' or 'Arizona'.
This SQL Server AND and OR condition example would delete all records from the employees table where the state is 'California'
and either the last_name is 'Johnson' or the first_name is 'Joe'.
Description
The SQL Server (Transact-SQL) DISTINCT clause is used to remove duplicates from the result set. The DISTINCT clause can only
be used with SELECT statements.
Syntax
The syntax for the DISTINCT clause in SQL Server (Transact-SQL) is:
Parameters or Arguments
expressions
The columns or calculations that you wish to retrieve.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
WHERE conditions
Compiled By : Amit Srivastava
Optional. The conditions that must be met for the records to be selected.
Note
• When only one expression is provided in the DISTINCT clause, the query will return the unique values for that expression.
• When more than one expression is provided in the DISTINCT clause, the query will retrieve unique combinations for the
expressions listed.
• In SQL Server, the DISTINCT clause doesn't ignore NULL values. So when using the DISTINCT clause in your SQL
statement, your result set will include NULL as a distinct value.
This SQL Server DISTINCT example would return all unique last_name values from the employees table where the employee_id is
greater than or equal to 50.
This SQL Server DISTINCT clause example would return each unique first_name and last_name combination from
the employees table where the employee_id is greater than or equal to 50. The results are sorted in ascending order by last_name.
In this case, the DISTINCT applies to each field listed after the DISTINCT keyword, and therefore returns distinct combinations.