To remove duplicate rows from a result set, you use the DISTINCT operator in the SELECT
SELECT
DISTINCT salary
FROM
employees
ORDER BY salary DESC;
------------------------
SQL top clause
To top the number of rows returned by a select statement
SELECT TOP 3 *
FROM
employees1;
-----------------------
SQL FETCH clause
The OFFSET FETCH clause allows you to skip N first rows in a result set before starting to return any rows.
The ROW and ROWS, FIRST and NEXT are the synonyms. Therefore, you can use them interchangeably.
The offset_rows is an integer number which must be zero or positive. In case the offset_rows is greater than the number of rows in the result set, no rows will be returned.
The fetch_rows is also an integer number that determines the number of rows to be returned. The value of fetch_rows is equal to or greater than one.
SELECT
*
FROM employees1
ORDER BY
salary DESC
OFFSET 0 ROWS
FETCH NEXT 1 ROWS ONLY;
...........
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY;
....
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY;
.........
OFFSET 2 ROWS
FETCH NEXT 1 ROWS ONLY;
---------------
SQL WHERE clause
To select specific rows from a table, you use a WHERE clause in the SELECT statement
It returns true if the value of the left expression is equal to the value of the right expression; otherwise, it returns false.
SELECT
employee_id, first_name, last_name
FROM
employees
WHERE
last_name = 'King';
Not equal to operator (<>)
The not equal to (<>) operator compares two non-null expressions and returns true if the value of the left expression is not equal to the right one; otherwise, it returns false.
For example, the following statement returns all employees whose department id is not 8.
SELECT
employee_id, first_name, last_name, department_id
FROM
employees
WHERE
department_id <> 8
Greater than operator (>)
The greater than operator (>) compares two non-null expressions and returns true if the left operand is greater than the right operand; otherwise, the result is false.
For example, to find the employees whose salary is greater than 10,000, you use the greater than operator in the WHERE clause as follows:
SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary > 10000
Less than operator (<)
The less than operator compares two non-null expressions. The result is true if the left operand evaluates to a value that is lower than the value of the right operand; otherwise, the result is false.
The following shows the syntax of the less than operator:
For example, the statement below returns all employees whose salaries are less than 10,000:
SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary < 10000
Greater than or equal operator (>=)
The greater than or equal operator (>=) compares two non-null expressions. The result is true if the left expression evaluates to a value that is greater than the value of the right expression.
The following illustrates the syntax of the greater than or equal operator:
The BETWEEN operator returns true if the expression is greater than or equal to ( >=) the low value and less than or equal to ( <=) the high value.
Technically, the BETWEEN is the equivalent to the following expression that uses the greater than or equal to (>=) and less than or equal to (<=) operators:
The NOT BETWEEN returns true if the expression is less than low or greater than (>) high; otherwise, it returns false.
Like the BETWEEN operator, you can rewrite the NOT BETWEEN operator using the less than (<) and greater than (>) operators with the OR operator as follows:
In practice, you often use the BETWEEN and NOT BETWEEN operator in the WHERE clause of the SELECT to select rows whose value of a column is within a specific range.
SQL BETWEEN operator examples
We’ll use the employees table from the sample database to illustrate how the BETWEEN operator works.
1) Using the SQL BETWEEN opeator with numbers example
The following statement uses the BETWEEN operator to find all employees whose salaries are between 2,500 and 2,900:
SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
salary BETWEEN2500AND2900ORDERBY
salary DESC;
Notice that the result set includes the employees whose salaries are 2,500 and 2,900.
The following query returns the same result set as the above query. However, it uses comparison operators greater than or equal to (>=) and less than or equal to (<=) instead:
SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
salary >= 2500AND salary <= 2900ORDERBY
salary;
Technically, you can substitute the IN operator with the = and OR operators The condition that uses the IN operator can be rewritten using one or more OR operators as follows:
Notice that if any value in the list (value1,value2,...) is null, the IN operator returns no rows.
In practice, you often use the IN and NOT IN operators in the WHERE clause of the SELECT statement to select rows with a value in a set of values. Also, you’ll use the IN operator in subqueries.
SQL IN examples
We will use the employees table in the sample database to demonstrate the functionality of the IN operator.
The following example uses the IN operator to find employees with the job id is 8, 9, or 10:
SELECT
employee_id,
first_name,
last_name,
job_id
FROM
employees
WHERE
job_id IN (8, 9, 10)
ORDERBY
job_id;
Comments
Post a Comment