SQL Joins and Views
SQL Joins and Views
SQL Joins and Views
SQL JOIN
The JOIN keyword is used in an SQL statement to query data from two or more tables,
based on a relationship between certain columns in these tables.
A primary key is a column (or a combination of columns) with a unique value for each
row. Each primary key value must be unique within the table. The purpose is to bind data
together, across tables, without repeating all of the data in every table.
Note that the "P_Id" column is the primary key in the "Persons" table. This means that no
two rows can have the same P_Id. The P_Id distinguishes two persons even if they have
the same name.Next, we have the "Orders" table:
Note that the "O_Id" column is the primary key in the "Orders" table and that the "P_Id"
column refers to the persons in the "Persons" table without using their names.Notice that
the relationship between the two tables above is the "P_Id" column.
Now we want to list all the persons with any orders.We use the following SELECT
statement:
The INNER JOIN keyword return rows when there is at least one match in both tables. If
there are rows in "Persons" that do not have matches in "Orders", those rows will NOT be
listed.
Now we want to list all the persons and their orders - if any, from the tables above.We
use the following SELECT statement:
The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there
are no matches in the right table (Orders).
Now we want to list all the orders with containing persons - if any, from the tables
above.We use the following SELECT statement:
The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if
there are no matches in the left table (Persons).
In SQL, a view is a virtual table based on the result-set of an SQL statement.A view
contains rows and columns, just like a real table. The fields in a view are fields from one
or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present the
data as if the data were coming from one single table.
SQL CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Note: A view always shows up-to-date data! The database engine recreates the data,
using the view's SQL statement, every time a user queries a view.
The view "Current Product List" lists all active products (products that are not
discontinued) from the "Products" table. The view is created with the following SQL:
Another view in the Northwind sample database selects every product in the "Products"
table with a unit price higher than the average unit price:
Another view in the Northwind database calculates the total sale for each category in
1997. Note that this view selects its data from another view called "Product Sales for
1997":
We can also add a condition to the query. Now we want to see the total sale only for the
category "Beverages":
Now we want to add the "Category" column to the "Current Product List" view. We will
update the view with the following SQL:
The AND & OR operators are used to filter records based on more than one condition.
Now we want to select only the persons with the first name equal to "Tove" AND the last
name equal to "Svendson":We use the following SELECT statement:
OR Operator Example
Now we want to select only the persons with the first name equal to "Tove" OR the first
name equal to "Ola":We use the following SELECT statement:
Sample SP
IF EXISTS (
SELECT * FROM Schedule WHERE SerialNumber=@SerialNumber AND Start IS NOT
NULL
)
BEGIN
SELECT @ReturnParameter='1'
END
ELSE IF EXISTS (
SELECT * FROM Schedule WHERE SerialNumber=@SerialNumber AND Start IS NOT
NULL
)
BEGIN
SELECT @ReturnParameter='2'
END
ELSE
BEGIN
SELECT @ReturnParameter=NULL
END
PRINT @ReturnParameter