0% found this document useful (0 votes)
30 views29 pages

More SQL Ii

The document discusses various SQL operators and concepts: 1) The IN operator allows selecting rows where a column's value matches any value in a list, rather than using multiple OR conditions. 2) SQL joins combine data from two or more tables based on relationships between their columns. Common join types are inner, left, right, and full outer joins. 3) Stored procedures allow saving SQL statements that can be reused with optional parameters, improving efficiency.

Uploaded by

cleophacerevival
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views29 pages

More SQL Ii

The document discusses various SQL operators and concepts: 1) The IN operator allows selecting rows where a column's value matches any value in a list, rather than using multiple OR conditions. 2) SQL joins combine data from two or more tables based on relationships between their columns. Common join types are inner, left, right, and full outer joins. 3) Stored procedures allow saving SQL statements that can be reused with optional parameters, improving efficiency.

Uploaded by

cleophacerevival
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

MORE SQL II

THE IN OPERATOR
The IN operator is used when you want to compare a
column with more than one value.

Suppose we have a Customers table in which one


column has the city where the customers live e.g.
London, New York, Stockholm, Nairobi etc..
THE IN OPERATOR…
For example, you might need to select all customers
from New York, Los Angeles, and Chicago.
With the OR condition, your SQL would look like this:
SELECT * FROM Customers WHERE
City=‘New York’
OR City=‘Los Angeles’
OR City=‘Chicago;
THE IN OPERATOR…
The resulting table would be
THE IN OPERATOR…
You can achieve the same result with a single IN
condition, instead of the multiple OR conditions:

SELECT * FROM Customers WHERE City IN (‘New


York’, ‘Los Angeles’, ‘Chicago’);
The NOT IN Operator
The NOT IN operator allows you to exclude a list of
specific values from the result set.

If we add the NOT keyword before IN in our previous


query, customers living in those cities will be
excluded:

SELECT * FROM Cutomers WHERE City NOT IN (‘New


York’, ‘Los Angeles’, ‘Chicago’);
SUBQUERIES
A subquery is a query within another query.

Let's consider an example. We might need the list of


all employees whose salaries are greater than the
average.
First, calculate the average:
SELECT AVG(Salary) FROM employees;
Then
SELECT firstName,Name FROM employees WHERE
Salary>3100 ORDER BY Salary Desc;
SUBQUERIES…
Alternatively , A single subquery will return the same
result more easily.

SELECT Firstname, Salary FROM employees WHERE


Salary>(SELECT AVG(Salary) FROM employees)
ORDER BY Salary DESC;
SQL JOINS
So far our queries have been limited to a single table. But
in real life a database contains multiple tables, and there
may be a need to select data from multiple tables.

A JOIN clause is used to combine rows from two or more


tables, based on a related column between them.

Consider the Customers and the Orders tables below


SQL JOINS…
CustomerID CustomerName ContactName Country

1 Alfred John Maria Anders Germany

2 Ana Tamimu Ana Trujillo Mexico


Customers

3 Antonio Isaac Antonio Mexico


Moreno

OrderID CustomerID OrderDate


10308 2 1996-09-18 Orders
10309 37 1996-09-19
10310 77 1996-09-20
SQL JOINS…
Notice that the "CustomerID" column in the "Orders"
table refers to the "CustomerID" in the "Customers" table.
The relationship between the two tables above is the
"CustomerID" column.
Then, we can create the following SQL statement (that
contains an INNER JOIN), that selects records that have
matching values in both tables:
SELECT *
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Custo
mers.CustomerID;
SQL JOINS
The resulting table

OrderID CustomerI OrderDa Custome Custome ContactNa Country


D te rId rName me
10308 2 1996-09- 2 Ana Ana Trujillo Mexico
18 Tamimu

If we want to select only specific columns e.g. CustomerName and OrderDate,


the query would be:

SELECT Customers.CustomerName, Orders.OrderDate


FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
Different Types of SQL JOINs

Here are the different types of the JOINs in SQL:


(INNER) JOIN: Returns records that have matching
values in both tables
LEFT (OUTER) JOIN: Returns all records from the left
table, and the matched records from the right table
RIGHT (OUTER) JOIN: Returns all records from the
right table, and the matched records from the left
table
FULL (OUTER) JOIN: Returns all records when there
is a match in either left or right table
Different Types of SQL JOINs
SQL INNER JOIN
The INNER JOIN keyword selects records that have
matching values in both tables.
INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
SQL INNER JOIN…
Example:
SELECT *
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.C
ustomerID;
This query would produce the table below.
SQL LEFT JOIN
The LEFT JOIN keyword returns all records from the left
table (table1), and the matching records from the right
table (table2). The result is 0 records from the right side,
if there is no match.
LEFT JOIN Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
SQL LEFT JOIN…
Example:
Using the Customers and Orders table we can run the
following query;
SELECT *
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID=Customer
s.CustomerID;

This query will produce the following table in the next


slide. Note that those rows in the second table that have no
matching records on left table are filled with NULL
SQL LEFT JOIN…
Resulting Table

OrderID Customer OrderDat Customer Customer ContactN Country


ID e ID Name ame
10308 2 1996-09- 2 Ana Ana Mexico
18 Tamimu Trujillo

10309 37 1996-09- NULL NULL NULL NULL


19
10310 77 1996-09- NULL NULL NULL NULL
20
SQL RIGHT JOIN
The RIGHT JOIN keyword returns all records from the
right table (table2), and the matching records from the
left table (table1). The result is 0 records from the left
side, if there is no match.
RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
SQL RIGHT JOIN…
Using the Customers and Orders table we can run the
following query;
SELECT *
FROM Orders
RIGHT JOIN Customers ON Orders.CustomerID=Custo
mers.CustomerID;
SQL RIGHT JOIN…
The resulting table:

OrderID Custome OrderDat Custome Customer ContactN Country


rID e rID Name ame
10308 2 1996-09- 2 Ana Ana Mexico
18 Tamimu Trujillo
NULL NULL NULL 1 Alfred Maria German
John Andres y
NULL NULL NULL 3 Antonio Antonio Mexico
Isac Moreno
SQL FULL OUTER JOIN
The FULL OUTER JOIN keyword returns all records
when there is a match in left (table1) or right (table2)
table records.
Tip: FULL OUTER JOIN and FULL JOIN are the same.
FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
SQL FULL OUTER JOIN…
Using the Customers and Orders table we can run the
following query;
SELECT *
FROM Orders
FULL JOIN Customers ON Orders.CustomerID=Custom
ers.CustomerID;
SQL FULL OUTER JOIN…
The resulting table

OrderI CustomerID Order Custome Custom ContactN Country


D Date rID erName ame
10308 2 1996- 2 Ana Ana Mexico
09-18 Tamimu Trujillo
10309 37 1996- NULL NULL NULL NULL
09-19
10310 77 1996- NULL NULL NULL NULL
09-20
NULL NULL NULL 1 Alfred Maria Germany
John Andres
NULL NULL NULL 3 Antonio Antonio Mexico
Isac Moreno
SQL STORED PROCEDURE
What is a Stored Procedure?
A stored procedure is a prepared SQL code that you can
save, so the code can be reused over and over again (It is
stored in the DBMS).
So if you have an SQL query that you write over and over
again, save it as a stored procedure, and then just call it
to execute it.
You can also pass parameters to a stored procedure, so
that the stored procedure can act based on the
parameter value(s) that is passed.
SQL STORED PROCEDURE…
Syntax (This syntax is specific for SQL Server DBMS):

CREATE PROCEDURE procedure_name


AS
sql_statement
GO;

After creating the procedure, you can Execute a Stored


Procedure with the command.

EXEC procedure_name;
SQL STORED PROCEDURE…
Stored Procedure Example

The following SQL statement creates a stored procedure named


"SelectAllCustomers" that selects all records from the "Customers" table:

CREATE PROCEDURE SelectAllCustomers


AS
SELECT * FROM Customers
GO;

To execute the stored procedure above run the command.


EXEC SelectAllCustomers;
More Reading
SQL is a wide topic, but a very important one.

Go to https://www.w3schools.com/sql/ and read more on the following SQL


concepts, and make your own notes.

• SELECT DISTINCT
• SELECT TOP
• GROUP BY
• HAVING

You might also like