SQL INDIVIDUAL TRAINING - 3

SQL Trigger

CREATE TABLE EmpLog (

LogID int IDENTITY(1,1) NOT NULL,

EmpID int NOT NULL,

Operation nvarchar(10) NOT NULL,

UpdatedDate Datetime NOT NULL

)

CREATE TABLE [Employee]

(

 [EmployeeID] [int] IDENTITY(1,1) NOT NULL,

[Name] [nchar](10) NULL

)


    SELECT EmployeeID ,'INSERT',GETDATE() FROM INSERTED;



INSERT INTO [dbo].[Employee]

           ([Name])

     VALUES

           ('test')



----------------------------------------




Introduction to the SQL INNER JOIN clause

So far, you have learned how to use the SELECT statement to query data from a single table. However, the SELECT statement is not limited to query data from a single table. The SELECT statement can link multiple tables together.

The process of linking tables is called joining. SQL provides many kinds of joins such as inner join, left join, right join, full outer join, etc. This tutorial focuses on the inner join.

Suppose, you have two tables: A and B.

Table A has four rows: (1,2,3,4) and table B has four rows: (3,4,5,6)

When table A joins with table B using the inner join, you have the result set (3,4) that is the intersection of table A and table B.

See the following picture.

SQL INNER JOIN

For each row in table A, the inner join clause finds the matching rows in table B. If a row is matched, it is included in the final result set.

1) Using SQL INNER JOIN to join two tables example

SELECT first_name, last_name, employees.department_id, departments.department_id, department_name FROM employees INNER JOIN departments ON departments.department_id = employees.department_id WHERE employees.department_id IN (1 , 2, 3);
Code language: SQL (Structured Query Language) (sql)

Try It

SQL INNER JOIN example

For each row in the employees table, the statement checks if the value of the department_id column equals the value of the department_id column in the departments table. If the condition

If the condition employees.department_id = departments.department_id is satisfied, the combined row that includes data from rows in both employees and departments tables are included in the result set.

Notice that both employees and departments tables have the same column name department_id, therefore we had to qualify the department_id column using the syntax table_name.column_name.

SQL INNER JOIN 3 tables example

Each employee holds one job while a job may be held by many employees. The relationship between the jobs table and the employees table is one-to-many.

The following database diagram illustrates the relationships between employeesdepartments and jobs tables:

emp_dept_jobs_tables

The following query uses the inner join clauses to join 3 tables: employees, departments, and jobs to get the first name, last name, job title, and department name of employees who work in department id 1, 2, and 3.

SELECT first_name, last_name, job_title, department_name FROM employees e INNER JOIN departments d ON d.department_id = e.department_id INNER JOIN jobs j ON j.job_id = e.job_id WHERE e.department_id IN (1, 2, 3);
Code language: SQL (Structured Query Language) (sql)

Try It

SQL INNER JOIN 3 tables example

Introduction to SQL LEFT JOIN clause

In the previous tutorial, you learned about the inner join that returns rows if there is, at least, one row in both tables that matches the join condition. The inner join clause eliminates the rows that do not match with a row of the other table.

The left join, however, returns all rows from the left table whether or not there is a matching row in the right table.

Suppose we have two tables A and B. The table A has four rows 1, 2, 3 and 4. The table B also has four rows 3, 4, 5, 6.

When we join table A with table B, all the rows in table A (the left table) are included in the result set whether there is a matching row in the table B or not.

SQL LEFT JOIN
SELECT c.country_name, c.country_id, l.country_id, l.street_address, l.city FROM countries c LEFT JOIN locations l ON l.country_id = c.country_id WHERE c.country_id IN ('US', 'UK', 'CN')
Code language: SQL (Structured Query Language) (sql)

Try It

SQL LEFT JOIN two tables example

The condition in the WHERE clause is applied so that the statement only retrieves the data from the US, UK, and China rows.

Because we use the LEFT JOIN clause, all rows that satisfy the condition in the WHERE clause of the countries table are included in the result set.

For each row in the countries table, the LEFT JOIN clause finds the matching rows in the locations table.

If at least one matching row found, the database engine combines the data from columns of the matching rows in both tables.

In case there is no matching row found e.g., with the country_id CN, the row in the countries table is included in the result set and the row in the locations table is filled with NULL values.

Because non-matching rows in the right table are filled with the NULL values, you can apply the LEFT JOIN clause to miss-match rows between tables.

For example, to find the country that does not have any locations in the locations table, you use the following query:

SELECT country_name FROM countries c LEFT JOIN locations l ON l.country_id = c.country_id WHERE l.location_id IS NULL ORDER BY country_name;
Code language: SQL (Structured Query Language) (sql)

Try It

SQL LEFT JOIN with IS NULL example


Introduction to SQL FULL OUTER JOIN clause

In theory, a full outer join is the combination of a left join and a right join. The full outer join includes all rows from the joined tables whether or not the other table has the matching row.

If the rows in the joined tables do not match, the result set of the full outer join contains NULL values for every column of the table that lacks a matching row. For the matching rows, a single row that has the columns populated from the joined table is included in the result set.

The following statement illustrates the syntax of the full outer join of two tables:

SELECT column_list FROM A FULL OUTER JOIN B ON B.n = A.n;
Code language: SQL (Structured Query Language) (sql)

Note that the OUTER keyword is optional.

The following Venn diagram illustrates the full outer join of two tables.

SQL FULL OUTER JOIN


SQL FULL OUTER JOIN examples

Let’s take an example of using the FULL OUTER JOIN clause to see how it works.

First, create two new tablesbaskets and fruits for the demonstration. Each basket stores zero or more fruits and each fruit can be stored in zero or one basket.

CREATE TABLE fruits ( fruit_id INTEGER PRIMARY KEY, fruit_name VARCHAR (255) NOT NULL, basket_id INTEGER );
Code language: SQL (Structured Query Language) (sql)
CREATE TABLE baskets ( basket_id INTEGER PRIMARY KEY, basket_name VARCHAR (255) NOT NULL );
Code language: SQL (Structured Query Language) (sql)

Second, insert some sample data into the baskets and fruits tables.

INSERT INTO baskets (basket_id, basket_name) VALUES (1, 'A'), (2, 'B'), (3, 'C');
Code language: SQL (Structured Query Language) (sql)
INSERT INTO fruits ( fruit_id, fruit_name, basket_id ) VALUES (1, 'Apple', 1), (2, 'Orange', 1), (3, 'Banana', 2), (4, 'Strawberry', NULL);
Code language: SQL (Structured Query Language) (sql)

Third, the following query returns each fruit that is in a basket and each basket that has a fruit, but also returns each fruit that is not in any basket and each basket that does not have any fruit.

SELECT basket_name, fruit_name FROM fruits FULL OUTER JOIN baskets ON baskets.basket_id = fruits.basket_id;
Code language: SQL (Structured Query Language) (sql)
basket_name | fruit_name -------------+------------ A | Apple A | Orange B | Banana (null) | Strawberry C | (null)
Code language: SQL (Structured Query Language) (sql)

As you see, the basket C does not have any fruit and the Strawberry is not in any basket.

You can add a WHERE clause to the statement that uses the FULL OUTER JOIN clause to get more specific information.

For example, to find the empty basket, which does not store any fruit, you use the following statement:

SELECT basket_name, fruit_name FROM fruits FULL OUTER JOIN baskets ON baskets.basket_id = fruits.basket_id WHERE fruit_name IS NULL;
Code language: SQL (Structured Query Language) (sql)
basket_name | fruit_name -------------+------------ C | (null) (1 row)
Code language: SQL (Structured Query Language) (sql)

Similarly, if you want to see which fruit is not in any basket, you use the following statement:

SELECT basket_name, fruit_name FROM fruits FULL OUTER JOIN baskets ON baskets.basket_id = fruits.basket_id WHERE basket_name IS NULL;
Code language: SQL (Structured Query Language) (sql)
basket_name | fruit_name -------------+------------ (null) | Strawberry (1 row)


Introduction to SQL CROSS JOIN clause

A cross join is a join operation that produces the Cartesian product of two or more tables.

In Math, a Cartesian product is a mathematical operation that returns a product set of multiple sets.

For example, with two sets A {x,y,z} and B {1,2,3}, the Cartesian product of A x B is the set of all ordered pairs (x,1), (x,2), (x,3), (y,1) (y,2), (y,3), (z,1), (z,2), (z,3).

The following picture illustrates the Cartesian product of A and B:

Similarly, in SQL, a Cartesian product of two tables A and B is a result set in which each row in the first table (A) is paired with each row in the second table (B). Suppose the A table has n rows and the B table has m rows, the result of the cross join of the A and B tables have n x m rows.

The following picture illustrates the result of the cross join between the table A and table B. In this illustration, the table A has three rows 1, 2 and 3 and the table B also has three rows x, y and z. As the result, the Cartesian product has nine rows:

SQL CROSS JOIN


SQL CROSS JOIN example

We will create two new tables  for the demonstration of the cross join:

  •  sales_organization table stores the sale organizations.
  •  sales_channel table stores the sales channels.

The following statements create the sales_organization and sales_channel tables:

CREATE TABLE sales_organization ( sales_org_id INT PRIMARY KEY, sales_org VARCHAR (255) );
Code language: SQL (Structured Query Language) (sql)
CREATE TABLE sales_channel ( channel_id INT PRIMARY KEY, channel VARCHAR (255) );
Code language: SQL (Structured Query Language) (sql)

Suppose the company has two sales organizations that are Domestic and Export, which are in charge of sales in the domestic and international markets.

The following statement inserts two sales organizations into the sales_organization table:

INSERT INTO sales_organization (sales_org_id, sales_org) VALUES (1, 'Domestic'), (2, 'Export');
Code language: SQL (Structured Query Language) (sql)

The company can distribute goods via various channels such as wholesale, retail, eCommerce, and TV shopping. The following statement inserts sales channels into the sales_channel table:

INSERT INTO sales_channel (channel_id, channel) VALUES (1, 'Wholesale'), (2, 'Retail'), (3, 'eCommerce'), (4, 'TV Shopping');
Code language: SQL (Structured Query Language) (sql)

To find the all possible sales channels that a sales organization can have, you use the CROSS JOIN to join the sales_organization table with the sales_channel table as follows:

SELECT sales_org, channel FROM sales_organization CROSS JOIN sales_channel;
Code language: SQL (Structured Query Language) (sql)

Here is the result set:

SQL CROSS JOIN example

Introduction to SQL self-join

Sometimes, it is useful to join a table to itself. This type of join is known as the self-join.

We join a table to itself to evaluate the rows with other rows in the same table. To perform the self-join, we use either an inner join or left join clause.

Because the same table appears twice in a single query, we have to use the table aliases. The following statement illustrates how to join a table to itself.

SQL self-join examples

See the following employees table.

employees_table

The manager_id column specifies the manager of an employee. The following statement joins the employees table to itself to query the information of who reports to whom.

SELECT e.first_name || ' ' || e.last_name AS employee, m.first_name || ' ' || m.last_name AS manager FROM employees e INNER JOIN employees m ON m.employee_id = e.manager_id ORDER BY manager;
Code language: SQL (Structured Query Language) (sql)

Try It

SQL Self-join example

The president does not have any manager. In the employees table, the manager_id of the row that contains the president is NULL.

Because the inner join clause only includes the rows that have matching rows in the other table, therefore the president did not show up in the result set of the query above.

To include the president in the result set, we use the LEFT JOIN clause instead of the INNER JOIN clause as the following query.

SELECT e.first_name || ' ' || e.last_name AS employee, m.first_name || ' ' || m.last_name AS manager FROM employees e LEFT JOIN employees m ON m.employee_id = e.manager_id ORDER BY manager;
Code language: SQL (Structured Query Language) (sql)

Try It

SQL self-join with LEFT JOIN example

In this tutorial, you have learned how to use the INNER JOIN or LEFT JOIN clause to join table to itself.


SQL TRAINING - 8 & 9

 

Display Data From Multiple Tables Using Joins 

Write SELECT statements to access data from more than one table

View data that generally does not meet a join condition by using outer joins Join a table by using a self-join 

Introduction to the SQL INNER JOIN clause

So far, you have learned how to use the SELECT statement to query data from a single table. However, the SELECT statement is not limited to query data from a single table. The SELECT statement can link multiple tables together.

The process of linking tables is called joining. SQL provides many kinds of joins such as inner join, left join, right join, full outer join, etc. This tutorial focuses on the inner join.

Suppose, you have two tables: A and B.

Table A has four rows: (1,2,3,4) and table B has four rows: (3,4,5,6)

When table A joins with table B using the inner join, you have the result set (3,4) that is the intersection of table A and table B.

See the following picture.

SQL INNER JOIN

For each row in table A, the inner join clause finds the matching rows in table B. If a row is matched, it is included in the final result set.

1) Using SQL INNER JOIN to join two tables example

SELECT first_name, last_name, employees.department_id, departments.department_id, department_name FROM employees INNER JOIN departments ON departments.department_id = employees.department_id WHERE employees.department_id IN (1 , 2, 3);
Code language: SQL (Structured Query Language) (sql)

Try It

SQL INNER JOIN example

For each row in the employees table, the statement checks if the value of the department_id column equals the value of the department_id column in the departments table. If the condition

If the condition employees.department_id = departments.department_id is satisfied, the combined row that includes data from rows in both employees and departments tables are included in the result set.

Notice that both employees and departments tables have the same column name department_id, therefore we had to qualify the department_id column using the syntax table_name.column_name.

SQL INNER JOIN 3 tables example

Each employee holds one job while a job may be held by many employees. The relationship between the jobs table and the employees table is one-to-many.

The following database diagram illustrates the relationships between employeesdepartments and jobs tables:

emp_dept_jobs_tables

The following query uses the inner join clauses to join 3 tables: employees, departments, and jobs to get the first name, last name, job title, and department name of employees who work in department id 1, 2, and 3.

SELECT first_name, last_name, job_title, department_name FROM employees e INNER JOIN departments d ON d.department_id = e.department_id INNER JOIN jobs j ON j.job_id = e.job_id WHERE e.department_id IN (1, 2, 3);
Code language: SQL (Structured Query Language) (sql)

Try It

SQL INNER JOIN 3 tables example

Introduction to SQL LEFT JOIN clause

In the previous tutorial, you learned about the inner join that returns rows if there is, at least, one row in both tables that matches the join condition. The inner join clause eliminates the rows that do not match with a row of the other table.

The left join, however, returns all rows from the left table whether or not there is a matching row in the right table.

Suppose we have two tables A and B. The table A has four rows 1, 2, 3 and 4. The table B also has four rows 3, 4, 5, 6.

When we join table A with table B, all the rows in table A (the left table) are included in the result set whether there is a matching row in the table B or not.

SQL LEFT JOIN
SELECT c.country_name, c.country_id, l.country_id, l.street_address, l.city FROM countries c LEFT JOIN locations l ON l.country_id = c.country_id WHERE c.country_id IN ('US', 'UK', 'CN')
Code language: SQL (Structured Query Language) (sql)

Try It

SQL LEFT JOIN two tables example

The condition in the WHERE clause is applied so that the statement only retrieves the data from the US, UK, and China rows.

Because we use the LEFT JOIN clause, all rows that satisfy the condition in the WHERE clause of the countries table are included in the result set.

For each row in the countries table, the LEFT JOIN clause finds the matching rows in the locations table.

If at least one matching row found, the database engine combines the data from columns of the matching rows in both tables.

In case there is no matching row found e.g., with the country_id CN, the row in the countries table is included in the result set and the row in the locations table is filled with NULL values.

Because non-matching rows in the right table are filled with the NULL values, you can apply the LEFT JOIN clause to miss-match rows between tables.

For example, to find the country that does not have any locations in the locations table, you use the following query:

SELECT country_name FROM countries c LEFT JOIN locations l ON l.country_id = c.country_id WHERE l.location_id IS NULL ORDER BY country_name;
Code language: SQL (Structured Query Language) (sql)

Try It

SQL LEFT JOIN with IS NULL example


Introduction to SQL FULL OUTER JOIN clause

In theory, a full outer join is the combination of a left join and a right join. The full outer join includes all rows from the joined tables whether or not the other table has the matching row.

If the rows in the joined tables do not match, the result set of the full outer join contains NULL values for every column of the table that lacks a matching row. For the matching rows, a single row that has the columns populated from the joined table is included in the result set.

The following statement illustrates the syntax of the full outer join of two tables:

SELECT column_list FROM A FULL OUTER JOIN B ON B.n = A.n;
Code language: SQL (Structured Query Language) (sql)

Note that the OUTER keyword is optional.

The following Venn diagram illustrates the full outer join of two tables.

SQL FULL OUTER JOIN


SQL FULL OUTER JOIN examples

Let’s take an example of using the FULL OUTER JOIN clause to see how it works.

First, create two new tablesbaskets and fruits for the demonstration. Each basket stores zero or more fruits and each fruit can be stored in zero or one basket.

CREATE TABLE fruits ( fruit_id INTEGER PRIMARY KEY, fruit_name VARCHAR (255) NOT NULL, basket_id INTEGER );
Code language: SQL (Structured Query Language) (sql)
CREATE TABLE baskets ( basket_id INTEGER PRIMARY KEY, basket_name VARCHAR (255) NOT NULL );
Code language: SQL (Structured Query Language) (sql)

Second, insert some sample data into the baskets and fruits tables.

INSERT INTO baskets (basket_id, basket_name) VALUES (1, 'A'), (2, 'B'), (3, 'C');
Code language: SQL (Structured Query Language) (sql)
INSERT INTO fruits ( fruit_id, fruit_name, basket_id ) VALUES (1, 'Apple', 1), (2, 'Orange', 1), (3, 'Banana', 2), (4, 'Strawberry', NULL);
Code language: SQL (Structured Query Language) (sql)

Third, the following query returns each fruit that is in a basket and each basket that has a fruit, but also returns each fruit that is not in any basket and each basket that does not have any fruit.

SELECT basket_name, fruit_name FROM fruits FULL OUTER JOIN baskets ON baskets.basket_id = fruits.basket_id;
Code language: SQL (Structured Query Language) (sql)
basket_name | fruit_name -------------+------------ A | Apple A | Orange B | Banana (null) | Strawberry C | (null)
Code language: SQL (Structured Query Language) (sql)

As you see, the basket C does not have any fruit and the Strawberry is not in any basket.

You can add a WHERE clause to the statement that uses the FULL OUTER JOIN clause to get more specific information.

For example, to find the empty basket, which does not store any fruit, you use the following statement:

SELECT basket_name, fruit_name FROM fruits FULL OUTER JOIN baskets ON baskets.basket_id = fruits.basket_id WHERE fruit_name IS NULL;
Code language: SQL (Structured Query Language) (sql)
basket_name | fruit_name -------------+------------ C | (null) (1 row)
Code language: SQL (Structured Query Language) (sql)

Similarly, if you want to see which fruit is not in any basket, you use the following statement:

SELECT basket_name, fruit_name FROM fruits FULL OUTER JOIN baskets ON baskets.basket_id = fruits.basket_id WHERE basket_name IS NULL;
Code language: SQL (Structured Query Language) (sql)
basket_name | fruit_name -------------+------------ (null) | Strawberry (1 row)


Introduction to SQL CROSS JOIN clause

A cross join is a join operation that produces the Cartesian product of two or more tables.

In Math, a Cartesian product is a mathematical operation that returns a product set of multiple sets.

For example, with two sets A {x,y,z} and B {1,2,3}, the Cartesian product of A x B is the set of all ordered pairs (x,1), (x,2), (x,3), (y,1) (y,2), (y,3), (z,1), (z,2), (z,3).

The following picture illustrates the Cartesian product of A and B:

Similarly, in SQL, a Cartesian product of two tables A and B is a result set in which each row in the first table (A) is paired with each row in the second table (B). Suppose the A table has n rows and the B table has m rows, the result of the cross join of the A and B tables have n x m rows.

The following picture illustrates the result of the cross join between the table A and table B. In this illustration, the table A has three rows 1, 2 and 3 and the table B also has three rows x, y and z. As the result, the Cartesian product has nine rows:

SQL CROSS JOIN


SQL CROSS JOIN example

We will create two new tables  for the demonstration of the cross join:

  •  sales_organization table stores the sale organizations.
  •  sales_channel table stores the sales channels.

The following statements create the sales_organization and sales_channel tables:

CREATE TABLE sales_organization ( sales_org_id INT PRIMARY KEY, sales_org VARCHAR (255) );
Code language: SQL (Structured Query Language) (sql)
CREATE TABLE sales_channel ( channel_id INT PRIMARY KEY, channel VARCHAR (255) );
Code language: SQL (Structured Query Language) (sql)

Suppose the company has two sales organizations that are Domestic and Export, which are in charge of sales in the domestic and international markets.

The following statement inserts two sales organizations into the sales_organization table:

INSERT INTO sales_organization (sales_org_id, sales_org) VALUES (1, 'Domestic'), (2, 'Export');
Code language: SQL (Structured Query Language) (sql)

The company can distribute goods via various channels such as wholesale, retail, eCommerce, and TV shopping. The following statement inserts sales channels into the sales_channel table:

INSERT INTO sales_channel (channel_id, channel) VALUES (1, 'Wholesale'), (2, 'Retail'), (3, 'eCommerce'), (4, 'TV Shopping');
Code language: SQL (Structured Query Language) (sql)

To find the all possible sales channels that a sales organization can have, you use the CROSS JOIN to join the sales_organization table with the sales_channel table as follows:

SELECT sales_org, channel FROM sales_organization CROSS JOIN sales_channel;
Code language: SQL (Structured Query Language) (sql)

Here is the result set:

SQL CROSS JOIN example

Introduction to SQL self-join

Sometimes, it is useful to join a table to itself. This type of join is known as the self-join.

We join a table to itself to evaluate the rows with other rows in the same table. To perform the self-join, we use either an inner join or left join clause.

Because the same table appears twice in a single query, we have to use the table aliases. The following statement illustrates how to join a table to itself.

SQL self-join examples

See the following employees table.

employees_table

The manager_id column specifies the manager of an employee. The following statement joins the employees table to itself to query the information of who reports to whom.

SELECT e.first_name || ' ' || e.last_name AS employee, m.first_name || ' ' || m.last_name AS manager FROM employees e INNER JOIN employees m ON m.employee_id = e.manager_id ORDER BY manager;
Code language: SQL (Structured Query Language) (sql)

Try It

SQL Self-join example

The president does not have any manager. In the employees table, the manager_id of the row that contains the president is NULL.

Because the inner join clause only includes the rows that have matching rows in the other table, therefore the president did not show up in the result set of the query above.

To include the president in the result set, we use the LEFT JOIN clause instead of the INNER JOIN clause as the following query.

SELECT e.first_name || ' ' || e.last_name AS employee, m.first_name || ' ' || m.last_name AS manager FROM employees e LEFT JOIN employees m ON m.employee_id = e.manager_id ORDER BY manager;
Code language: SQL (Structured Query Language) (sql)

Try It

SQL self-join with LEFT JOIN example

In this tutorial, you have learned how to use the INNER JOIN or LEFT JOIN clause to join table to itself.



Use Sub-Queries to Solve Queries 

Describe the types of problem that sub-queries can solve 


Define sub-queries 

SQL subquery basic

Consider the following employees and departments tables from the sample database:

SQL Subquery: Sample tables

Suppose you have to find all employees who locate in the location with the id 1700. You might come up with the following solution.

First, find all departments located at the location whose id is 1700:

SELECT * FROM departments WHERE location_id = 1700;
Code language: SQL (Structured Query Language) (sql)
SQL Subquery - department list

Second, find all employees that belong to the location 1700 by using the department id list of the previous query:

SELECT employee_id, first_name, last_name FROM employees WHERE department_id IN (1 , 3, 8, 10, 11) ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)
SQL Subquery - IN operator

This solution has two problems. To start with, you have looked at the departments table to check which department belongs to the location 1700. However, the original question was not referring to any specific departments; it referred to the location 1700.

Because of the small data volume, you can get a list of department easily. However, in the real system with high volume data, it might be problematic.

Another problem was that you have to revise the queries whenever you want to find employees who locate in a different location.

A much better solution to this problem is to use a subquery. By definition, a subquery is a query nested inside another query such as SELECTINSERTUPDATE, or DELETE statement. In this tutorial, we are focusing on the subquery used with the SELECT statement.

In this example, you can rewrite combine the two queries above as follows:

SELECT employee_id, first_name, last_name FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1700) ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)

The query placed within the parentheses is called a subquery. It is also known as an inner query or inner select. The query that contains the subquery is called an outer query or an outer select.

To execute the query, first, the database system has to execute the subquery and substitute the subquery between the parentheses with its result – a number of department id located at the location 1700 – and then executes the outer query.

You can use a subquery in many places such as:

SQL subquery examples

Let’s take some examples of using the subqueries to understand how they work.

SQL subquery with the IN or NOT IN operator

In the previous example, you have seen how the subquery was used with the IN operator. The following example uses a subquery with the NOT IN operator to find all employees who do not locate at the location 1700:

SELECT employee_id, first_name, last_name FROM employees WHERE department_id NOT IN (SELECT department_id FROM departments WHERE location_id = 1700) ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)
SQL Subquery - NOT IN operator

SQL subquery with the comparison operator

The following syntax illustrates how a subquery is used with a comparison operator:

comparison_operator (subquery)
Code language: SQL (Structured Query Language) (sql)

where the comparison operator is one of these operators:

  • Equal (=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal ( >=)
  • Less than or equal (<=)
  • Not equal ( !=) or (<>)

The following example finds the employees who have the highest salary:

SELECT employee_id, first_name, last_name, salary FROM employees WHERE salary = (SELECT MAX(salary) FROM employees) ORDER BY first_name , last_name;
Code language: SQL (Structured Query Language) (sql)
SQL Subquery with the Equal operator

In this example, the subquery returns the highest salary of all employees and the outer query finds the employees whose salary is equal to the highest one.

The following statement finds all employees who salaries are greater than the average salary of all employees:

SELECT employee_id, first_name, last_name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
Code language: SQL (Structured Query Language) (sql)
SQL Subquery with greater than operator

In this example, first, the subquery returns the average salary of all employees. Then, the outer query uses the greater than operator to find all employees whose salaries are greater than the average.

SQL subquery with the EXISTS or NOT EXISTS operator

The EXISTS operator checks for the existence of rows returned from the subquery. It returns true if the subquery contains any rows. Otherwise, it returns false.

The syntax of the EXISTS operator is as follows:

EXISTS (subquery )
Code language: SQL (Structured Query Language) (sql)

The NOT EXISTS operator is opposite to the EXISTS operator.

NOT EXISTS (subquery)
Code language: SQL (Structured Query Language) (sql)

The following example finds all departments which have at least one employee with the salary is greater than 10,000:

SELECT department_name FROM departments d WHERE EXISTS( SELECT 1 FROM employees e WHERE salary > 10000 AND e.department_id = d.department_id) ORDER BY department_name;
Code language: SQL (Structured Query Language) (sql)

SQL Subquery with the EXISTS operator

List the types of sub-queries 

Write single-row and multiple-row sub-queries 

The SET Operators

Describe the SET operators 

Introduction to SQL UNION operator


The UNION operator combines result sets of two or more SELECT statements into a single result set. The following statement illustrates how to use the UNION operator to combine result sets of two queries:

To use the UNION operator, you write the dividual SELECT statements and join them by the keyword UNION.

The columns returned by the SELECT statements must have the same or convertible data type, size, and be the same order.

The database system processes the query by executing two SELECT statements first. Then, it combines two individual result sets into one and eliminates duplicate rows. To eliminate the duplicate rows, the database system sorts the combined result set by every column and scans it for the matching rows located next to one another.

To retain the duplicate rows in the result set, you use the UNION ALL operator.

Suppose, we have two result sets A(1,2) and B(2,3). The following picture illustrates A UNION B:

SQL UNION

And the following picture illustrates A UNION ALL B

SQL UNION ALL

The union is different from the join that the join combines columns of multiple tables while the union combines rows of the tables.

The database system performs the following steps:

  1. First, execute each SELECT statement individually.
  2. Second, combine result sets and remove duplicate rows to create the combined result set.
  3. Third, sort the combined result set by the column specified in the ORDER BY clause.

In practice, we often use the UNION operator to combine data from different tables. See the following employees and dependents tables:

employees_dependents_tables

The following statement uses the UNION operator to combine the first name and last name of employees and dependents.

SELECT first_name, last_name FROM employees UNION SELECT first_name, last_name FROM dependents ORDER BY last_name;
Code language: SQL (Structured Query Language) (sql)

Try It

SQL UNION practical example

In this tutorial, you have learned how to use the UNION operator to combine two or more result sets from multiple queries.

Introduction to SQL INTERSECT operator

The INTERSECT operator is a set operator that returns distinct rows of two or more result sets from SELECT statements.

Suppose, we have two tables: A(1,2) and B(2,3).

The following picture illustrates the intersection of A & B tables.

SQL-INTERSECT-Operator

The purple section is the intersection of the green and blue result sets.

Like the UNION operator, the INTERSECT operator removes the duplicate rows from the final result set.

To use the INTERSECT operator, the columns of the SELECT statements must follow the rules:

  • The data types of columns must be compatible.
  • The number of columns and their orders in the SELECT statements must be the same.

SQL INTERSECT with ORDER BY example

To sort the result set returned by the INTERSECT operator, you place the ORDER BY clause at the end of all statements.

For example, the following statement applies the INTERSECT operator to the A and B tables and sorts the combined result set by the id column in descending order.

SELECT id FROM a INTERSECT SELECT id FROM b ORDER BY id DESC;
Code language: SQL (Structured Query Language) (sql)
SQL INTERSECT with ORDER BY example

Emulate SQL INTERSECT operator using INNER JOIN clause

Most relational database system supports the INTERSECT operator such as Oracle Database, Microsoft SQL Server, PostgreSQL, etc. However, some database systems do not provide the INTERSECT operator like MySQL.

To emulate the SQL INTERSECT operator, you can use the INNER JOIN clause as follows:

SELECT a.id FROM a INNER JOIN b ON b.id = a.id
Code language: SQL (Structured Query Language) (sql)

It returns the rows in the A table that have matching rows in the B table, which produces the same result as the INTERSECT operator.


Introduction to SQL MINUS operator

Besides the UNIONUNION ALL, and INTERSECT operators, SQL provides us with the MINUS operator that allows you to subtract one result set from another result set.



To use the MINUS operator, you write individual SELECT statements and place the MINUS operator between them. The MINUS operator returns the unique rows produced by the first query but not by the second one.

The following picture illustrates the MINUS operator.

SQL MINUS

To make the result set, the database system performs two queries and subtracts the result set of the first query from the second one.

In order to use the MINUS operator, the columns in the SELECT clauses must match in number and must have the same or, at least, convertible data type.

We often use the MINUS operator in ETL. An ETL is a software component in data warehouse system. ETL stands for Extract, Transform, and Load. ETL is responsible for loading data from the source systems into the data warehouse system.

After complete loading data, we can use the MINUS operator to make sure that the data has been loaded fully by subtracting data in target system from the data in the source system.

Each employee has zero or more dependents while each dependent depends on one and only one employees. The relationship between the dependents and employees is the one-to-many relationship.

The employee_id column in the dependents table references to the employee_id column in the  employees table.

You can use the MINUS operator to find the employees who do not have any dependents. To do this, you subtract the employee_id result set in the  employees table from the employee_id result set in the dependents table.

The following query illustrates the idea:

SELECT employee_id FROM employees MINUS SELECT employee_id FROM dependents;


SQL  MINUS with ORDER BY example

To sort the result set returned by the MINUS operator, you place the ORDER BY clause at the end of the last SELECT statement.

For example, to sort the employees who do not have any dependents, you use the following query:

SELECT employee_id FROM employees MINUS SELECT employee_id FROM dependents ORDER BY employee_id;
Code language: SQL (Structured Query Language) (sql)
SQL MINUS ORDER BY example

Introduction to SQL INTERSECT operator

The INTERSECT operator is a set operator that returns distinct rows of two or more result sets from SELECT statements.

Suppose, we have two tables: A(1,2) and B(2,3).

The following picture illustrates the intersection of A & B tables.

SQL-INTERSECT-Operator

The purple section is the intersection of the green and blue result sets.

Like the UNION operator, the INTERSECT operator removes the duplicate rows from the final result set.

To use the INTERSECT operator, the columns of the SELECT statements must follow the rules:

  • The data types of columns must be compatible.
  • The number of columns and their orders in the SELECT statements must be the same.

SQL INTERSECT with ORDER BY example

To sort the result set returned by the INTERSECT operator, you place the ORDER BY clause at the end of all statements.

For example, the following statement applies the INTERSECT operator to the A and B tables and sorts the combined result set by the id column in descending order.

SELECT id FROM a INTERSECT SELECT id FROM b ORDER BY id DESC;
Code language: SQL (Structured Query Language) (sql)
SQL INTERSECT with ORDER BY example


Introduction to SQL MINUS operator

Besides the UNIONUNION ALL, and INTERSECT operators, SQL provides us with the MINUS operator that allows you to subtract one result set from another result set.



To use the MINUS operator, you write individual SELECT statements and place the MINUS operator between them. The MINUS operator returns the unique rows produced by the first query but not by the second one.

The following picture illustrates the MINUS operator.

SQL MINUS

To make the result set, the database system performs two queries and subtracts the result set of the first query from the second one.

In order to use the MINUS operator, the columns in the SELECT clauses must match in number and must have the same or, at least, convertible data type.

We often use the MINUS operator in ETL. An ETL is a software component in data warehouse system. ETL stands for Extract, Transform, and Load. ETL is responsible for loading data from the source systems into the data warehouse system.

After complete loading data, we can use the MINUS operator to make sure that the data has been loaded fully by subtracting data in target system from the data in the source system.

Each employee has zero or more dependents while each dependent depends on one and only one employees. The relationship between the dependents and employees is the one-to-many relationship.

The employee_id column in the dependents table references to the employee_id column in the  employees table.

You can use the MINUS operator to find the employees who do not have any dependents. To do this, you subtract the employee_id result set in the  employees table from the employee_id result set in the dependents table.

The following query illustrates the idea:

SELECT employee_id FROM employees MINUS SELECT employee_id FROM dependents;


SQL  MINUS with ORDER BY example

To sort the result set returned by the MINUS operator, you place the ORDER BY clause at the end of the last SELECT statement.

For example, to sort the employees who do not have any dependents, you use the following query:

SELECT employee_id FROM employees MINUS SELECT employee_id FROM dependents ORDER BY employee_id;
Code language: SQL (Structured Query Language) (sql)
SQL MINUS ORDER BY example

Data Manipulation Statements 

Save and discard changes with the COMMIT and ROLLBACK statements Explain read consistency 

Other Schema Objects 

Create a simple and complex view 

Retrieve data from views 

Introduction to the SQL Views

A relational database consists of multiple related tables e.g., employees, departments, jobs, etc. When you want to see the data of these tables, you use the SELECT statement with JOIN or UNION clauses.

SQL provides you with another way to see the data is by using the views.  A view is like a virtual table produced by executing a query. The relational database management system (RDBMS) stores a view as a named SELECT in the database catalog.

Whenever you issue a SELECT statement that contains a view name, the RDBMS executes the view-defining query to create the virtual table. That virtual table then is used as the source table of the query.

Why do you need to use the views

Views allow you to store complex queries in the database. For example, instead of issuing a complex SQL query each time you want to see the data, you just need to issue a simple query as follows:

SELECT column_list FROM view_name;
Code language: SQL (Structured Query Language) (sql)

Views help you pack the data for a specific group of users. For example, you can create a view of salary data for the employees for Finance department.

Views help maintain database security. Rather than give the users access to database tables, you create a view to revealing only necessary data and grant the users to access to the view.

Creating SQL views

To create a view, you use the CREATE VIEW statement as follows:

CREATE VIEW view_name AS SELECT-statement
Code language: SQL (Structured Query Language) (sql)

First, specify the name of the view after the CREATE VIEW clause.

Second, construct a SELECT statement to query data from multiple tables.

For example, the following statement creates the employee contacts view based on the data of the employees and departments tables.

emp_dept_tables
CREATE VIEW employee_contacts AS SELECT first_name, last_name, email, phone_number, department_name FROM employees e INNER JOIN departments d ON d.department_id = e.department_id ORDER BY first_name;
Code language: SQL (Structured Query Language) (sql)

By default, the names of columns of the view are the same as column specified in the SELECT statement. If you want to rename the columns in the view, you include the new column names after the CREATE VIEW clause as follows:

CREATE VIEW view_name(new_column_list) AS SELECT-statement;
Code language: SQL (Structured Query Language) (sql)

For example, the following statement creates a view whose column names are not the same as the column names of the base tables.

CREATE VIEW payroll (first_name , last_name , job, compensation) AS SELECT first_name, last_name, job_title, salary FROM employees e INNER JOIN jobs j ON j.job_id= e.job_id ORDER BY first_name;
Code language: SQL (Structured Query Language) (sql)

Querying data from views

Querying data from views is the same as querying data from tables. The following statement selects data from the employee_contacts view.

SELECT * FROM employee_contacts;
Code language: SQL (Structured Query Language) (sql)
SQL View Example

Of course, you can apply filtering or grouping as follows:

SELECT job, MIN(compensation), MAX(compensation), AVG(compensation) FROM payroll WHERE job LIKE 'A%' GROUP BY job;
Code language: SQL (Structured Query Language) (sql)
SQL Querying View

Modifying SQL views

To modify a view, either adding new columns to the view or removing columns from a view, you use the same CREATE OR REPLACE VIEW statement.

CREATE OR REPLACE view_name AS SELECT-statement;
Code language: SQL (Structured Query Language) (sql)

The statement creates a view if it does not exist or change the current view if the view already exists.

For example, the following statement changes the payroll view by adding the department column and rename the compensation column to salary column.

CREATE OR REPLACE VIEW payroll (first_name , last_name , job , department , salary) AS SELECT first_name, last_name, job_title, department_name, salary FROM employees e INNER JOIN jobs j ON j.job_id = e.job_id INNER JOIN departments d ON d.department_id = e.department_id ORDER BY first_name;
Code language: SQL (Structured Query Language) (sql)
SELECT * FROM payroll;
Code language: SQL (Structured Query Language) (sql)
SQL Modify View Example

Removing SQL views

To remove a view from the database, you use the DROP VIEW statement:

DROP VIEW view_name;
Code language: SQL (Structured Query Language) (sql)

The DROP VIEW statement deletes the view only, not the base tables.

For example, to remove the payroll view, you use the following statement:

DROP VIEW payroll;

Create and maintain indexes 

Why SQL Index?

The following reasons tell why Index is necessary in SQL:

  • SQL Indexes can search the information of the large database quickly.
  • This concept is a quick process for those columns, including different values.
  • This data structure sorts the data values of columns (fields) either in ascending or descending order. And then, it assigns the entry for each value.
  • Each Index table contains only two columns. The first column is row_id, and the other is indexed-column.
  • When indexes are used with smaller tables, the performance of the index may not be recognized.

Create an INDEX

In SQL, we can easily create the Index using the following CREATE Statement:

  1. CREATE INDEX Index_Name ON Table_Name ( Column_Name);  

Here, Index_Name is the name of that index that we want to create, and Table_Name is the name of the table on which the index is to be created. The Column_Name represents the name of the column on which index is to be applied.

  1. CREATE INDEX index_state ON Employee (Emp_State);  

Suppose we want to create an index on the combination of the Emp_city and the Emp_State column of the above Employee table. For this, we have to use the following query:

  1. CREATE INDEX index_city_State ON Employee (Emp_City, Emp_State);  

Create UNIQUE INDEX

Unique Index is the same as the Primary key in SQL. The unique index does not allow selecting those columns which contain duplicate values.

This index is the best way to maintain the data integrity of the SQL tables.

Syntax for creating the Unique Index is as follows:

  1. CREATE UNIQUE INDEX Index_Name ON Table_Name ( Column_Name);  

Introduction to SQL Server clustered indexes

The following statement creates a new table named production.parts that consists of two columns part_id and part_name:

CREATE TABLE production.parts( part_id INT NOT NULL, part_name VARCHAR(100) );
Code language: CSS (css)

And this statement inserts some rows into the production.parts table:

INSERT INTO production.parts(part_id, part_name) VALUES (1,'Frame'), (2,'Head Tube'), (3,'Handlebar Grip'), (4,'Shock Absorber'), (5,'Fork');
Code language: JavaScript (javascript)

The production.parts table does not have a primary key. Therefore SQL Server stores its rows in an unordered structure called a heap.

When you query data from the production.parts table, the query optimizer needs to scan the whole table to search.

For example, the following SELECT statement finds the part with id 5:

SELECT part_id, part_name FROM production.parts WHERE part_id = 5;

If you display the estimated execution plan in SQL Server Management Studio, you’ll see how SQL Server come up with the following query plan:

Note that to display the estimated execution plan in SQL Server Management Studio, you click the Display Estimated Execution Plan button or select the query and press the keyboard shortcut Ctrl+L:

SQL Server Display Estimated Execution Plan

Because the production.parts table has only five rows, the query executes very fast. However, if the table contains a large number of rows, it’ll take a lot of time and resources to search for data.

To resolve this issue, SQL Server provides a dedicated structure to speed up the retrieval of rows from a table called index.

SQL Server has two types of indexes: clustered index and non-clustered index. We will focus on the clustered index in this tutorial.

A clustered index stores data rows in a sorted structure based on its key values. Each table has only one clustered index because data rows can be only sorted in one order. A table that has a clustered index is called a clustered table.

The following picture illustrates the structure of a clustered index:

A clustered index organizes data using a special structured so-called B-tree (or balanced tree) which enables searches, inserts, updates and deletes in logarithmic amortized time.

In this structure, the top node of the B-tree is called the root node. The nodes at the bottom level are called the leaf nodes. Any index levels between the root and the leaf nodes are known as intermediate levels.

In the B-Tree, the root node and intermediate level nodes contain index pages that hold index rows. The leaf nodes contain the data pages of the underlying table. The pages in each level of the index are linked using another structure called a doubly-linked list.

SQL Server Clustered Index and Primary key constraint

When you create a table with a primary key, SQL Server automatically creates a corresponding clustered index that includes primary key columns.

This statement creates a new table named production.part_prices with a primary key that includes two columns: part_id and valid_from.

CREATE TABLE production.part_prices( part_id int, valid_from date, price decimal(18,4) not null, PRIMARY KEY(part_id, valid_from) );
Code language: JavaScript (javascript)

If you add a primary key constraint to an existing table that already has a clustered index, SQL Server will enforce the primary key using a non-clustered index:

This statement defines a primary key for the production.parts table:

ALTER TABLE production.parts ADD PRIMARY KEY(part_id);
Code language: CSS (css)

SQL Server created a non-clustered index for the primary key.

Using SQL Server CREATE CLUSTERED INDEX statement to create a clustered index.

When a table does not have a primary key, which is very rare, you can use the CREATE CLUSTERED INDEX statement to add a clustered index to it.

The following statement creates a clustered index for the production.parts table:

CREATE CLUSTERED INDEX ix_parts_id ON production.parts (part_id);
Code language: CSS (css)

If you open the Indexes node under the table name, you will see the new index name ix_parts_id with type Clustered.

When executing the following statement, SQL Server traverses the index (Clustered Index Seek) to locate the rows, which is faster than scanning the whole table.

SELECT part_id, part_name FROM production.parts WHERE part_id = 5;



Table Variable
 
A Table Variable is a variable that can store the complete table of the data inside it. It is similar to a Table Variable but as I said a Table Variable is a variable. So how do we declare a variable in SQL? Using the @ symbol. The same is true for a Table Variable. so the syntax of the Table Variable is as follows:

  1. Declare @TempTable TABLE(      
  2. id int,    
  3. Name varchar(20)      
  4. )      
  5.   
  6. insert into @TempTable values(1,'Sourabh Somani')  
  7. insert into @TempTable values(2,'Shaili Dashora')  
  8. insert into @TempTable values(3,'Divya Sharma')  
  9. insert into @TempTable values(4,'Swati Soni')  
  10.   
  11. Select * from @TempTable  


  12.  
    Difference between temporary tables and Table Variable
     
    There are a difference between temporary tables and temporary variables, it is:
    1. A Table Variable is not available after execution of the complete query so you cannot run a single query but a temporary table is available after executing the query.

      For example:

      temporary tables and temporary variables

    2. A Transaction (Commit and Rollback) operation is not possible in a Table Variable but in a temporary table we can perform transactiona (Commit and Rollback).

      For example:
      1. Declare @TempTable TABLE(      
      2. id int,    
      3. Name varchar(20)      
      4. )      
      5. begin tran T  
      6. insert into @TempTable values(1,'Sourabh Somani')  
      7. insert into @TempTable values(2,'Shaili Dashora')  
      8. insert into @TempTable values(3,'Divya Sharma')  
      9. insert into @TempTable values(4,'Swati Soni')  
      10. commit tran T  
      11. Select * from @TempTable  
      Transaction

      or 
      1. Declare @TempTable TABLE(      
      2. id int,    
      3. Name varchar(20)      
      4. )      
      5. begin tran T  
      6. insert into @TempTable values(1,'Sourabh Somani')  
      7. insert into @TempTable values(2,'Shaili Dashora')  
      8. insert into @TempTable values(3,'Divya Sharma')  
      9. insert into @TempTable values(4,'Swati Soni')  
      10. rollback tran T  
      11. Select * from @TempTable  
      Transaction operation
    Important Points about Table Variables
    • The same as a temporary table.
    • Single query cannot be executed.
    • When we want to perform a few operations then use a Table Variable otherwise if it is a huge amount of data operation then use a temporary table.
    • Commit and Rollback (Transaction) cannot be possible with Table Variables so if you want to perform a transaction operation then always go with temporary tables.

      What are scalar functions

      SQL Server scalar function takes one or more parameters and returns a single value.

      The scalar functions help you simplify your code. For example, you may have a complex calculation that appears in many queries. Instead of including the formula in every query, you can create a scalar function that encapsulates the formula and uses it in each query.

      Creating a scalar function

      To create a scalar function, you use the CREATE FUNCTION statement as follows:

      CREATE FUNCTION [schema_name.]function_name (parameter_list) RETURNS data_type AS BEGIN statements RETURN value END
      Code language: SQL (Structured Query Language) (sql)

      In this syntax:

      • First, specify the name of the function after the CREATE FUNCTION keywords. The schema name is optional. If you don’t explicitly specify it, SQL Server uses dbo by default.
      • Second, specify a list of parameters surrounded by parentheses after the function name.
      • Third, specify the data type of the return value in the RETURNS statement.
      • Finally, include a RETURN statement to return a value inside the body of the function.

      The following example creates a function that calculates the net sales based on the quantity, list price, and discount:

      CREATE FUNCTION sales.udfNetSale( @quantity INT, @list_price DEC(10,2), @discount DEC(4,2) ) RETURNS DEC(10,2) AS BEGIN RETURN @quantity * @list_price * (1 - @discount); END;
      Code language: SQL (Structured Query Language) (sql)

      Later on, we can use this to calculate net sales of any sales order in the order_items from the sample database.

      order_items table

      After creating the scalar function, you can find it under Programmability > Functions > Scalar-valued Functions as shown in the following picture:

      SQL Server Scalar Function

      Calling a scalar function

      You call a scalar function like a built-in function. For example, the following statement demonstrates how to call the udfNetSale function:

      SELECT sales.udfNetSale(10,100,0.1) net_sale;
      Code language: SQL (Structured Query Language) (sql)

      Here is the output:

      SQL Server Scalar Function example

      The following example illustrates how to use the sales.udfNetSale function to get the net sales of the sales orders in the order_items table:

      SELECT order_id, SUM(sales.udfNetSale(quantity, list_price, discount)) net_amount FROM sales.order_items GROUP BY order_id ORDER BY net_amount DESC;
      Code language: SQL (Structured Query Language) (sql)

      The following picture shows the partial output:

      SQL Server Scalar Function - calling function

      Modifying a scalar function

      To modify a scalar function, you use the ALTER instead of the CREATE keyword. The rest statements remain the same:

      ALTER FUNCTION [schema_name.]function_name (parameter_list) RETURN data_type AS BEGIN statements RETURN value END
      Code language: SQL (Structured Query Language) (sql)

      Note that you can use the CREATE OR ALTER statement to create a user-defined function if it does not exist or to modify an existing scalar function:

      CREATE OR ALTER FUNCTION [schema_name.]function_name (parameter_list) RETURN data_type AS BEGIN statements RETURN value END
      Code language: SQL (Structured Query Language) (sql)

      Removing a scalar function

      To remove an existing scalar function, you use the DROP FUNCTION statement:

      DROP FUNCTION [schema_name.]function_name;
      Code language: SQL (Structured Query Language) (sql)

      For example, to remove the sales.udfNetSale function, you use the following statement:

      DROP FUNCTION sales.udfNetSale;
      Code language: SQL (Structured Query Language) (sql)

      SQL Server scalar function notes

      The following are some key takeaway of the scalar functions:

      • Scalar functions can be used almost anywhere in T-SQL statements.
      • Scalar functions accept one or more parameters but return only one value, therefore, they must include a RETURN statement.
      • Scalar functions can use logic such as IF blocks or WHILE loops.
      • Scalar functions cannot update data. They can access data but this is not a good practice.
      • Scalar functions can call other functions

      What is a table-valued function in SQL Server

      A table-valued function is a user-defined function that returns data of a table type. The return type of a table-valued function is a table, therefore, you can use the table-valued function just like you would use a table.

      Creating a table-valued function

      The following statement example creates a table-valued function that returns a list of products including product name, model year and the list price for a specific model year:

      CREATE FUNCTION udfProductInYear ( @model_year INT ) RETURNS TABLE AS RETURN SELECT product_name, model_year, list_price FROM production.products WHERE model_year = @model_year;
      Code language: SQL (Structured Query Language) (sql)

      The syntax is similar to the one that creates a user-defined function.

      The RETURNS TABLE specifies that the function will return a table. As you can see, there is no BEGIN...END statement. The statement simply queries data from the production.products table.

      The udfProductInYear function accepts one parameter named @model_year of type INT. It returns the products whose model years equal @model_year parameter.

      Once the table-valued function is created, you can find it under Programmability > Functions > Table-valued Functions as shown in the following picture:

      SQL Server Table-valued Function example

      The function above returns the result set of a single SELECT statement, therefore, it is also known as an inline table-valued function.

      Executing a table-valued function

      To execute a table-valued function, you use it in the FROM clause of the SELECT statement:

      SELECT * FROM udfProductInYear(2017);
      Code language: SQL (Structured Query Language) (sql)
      SQL Server Table-valued Function Execution

      In this example, we selected the products whose model year is 2017.

      You can also specify which columns to be returned from the table-valued function as follows:

      SELECT product_name, list_price FROM udfProductInYear(2018);
      Code language: SQL (Structured Query Language) (sql)

      Here is the partial output:

      SQL Server Table-valued Function - Selecting columns

      Modifying a table-valued function

      To modify a table-valued function, you use the ALTER instead of CREATE keyword. The rest of the script is the same.

      For example, the following statement modifies the udfProductInYear by changing the existing parameter and adding one more parameter:

      ALTER FUNCTION udfProductInYear ( @start_year INT, @end_year INT ) RETURNS TABLE AS RETURN SELECT product_name, model_year, list_price FROM production.products WHERE model_year BETWEEN @start_year AND @end_year
      Code language: SQL (Structured Query Language) (sql)

      The udfProductInYear function now returns products whose model year between a starting year and an ending year.

      The following statement calls the udfProductInYear function to get the products whose model years are between 2017 and 2018:

      SELECT product_name, model_year, list_price FROM udfProductInYear(2017,2018) ORDER BY product_name;
      Code language: SQL (Structured Query Language) (sql)

      Here is the partial output:

      SQL Server Table-valued Function Modifying

      Comments

      Popular Posts