SQL 2
SQL 2
SQL 2
StudrecordResult
– SELECT Customer,OrderDate,SUM(OrderPrice)
FROM OrdersGROUP BY Customer,OrderDate
SQL HAVING Clause
• SELECT column_name,
aggregate_function(column_name)FROM
table_name GROUP BY column_name
HAVING aggregate_function(column_name)
operator value
LO4: Write and execute SQL sub
Queries
1. Single Vs. nested queries
2. What is the use of subquery
3. How do the subqueries used
4. Identify the operators used in SQL
subqueries
5. Inner query Vs. Outer query
Index
• An index can be created in a table to find data
more quickly and efficiently.
• The users cannot see the indexes, they are just
used to speed up searches/queries
• An index in a database is very similar to an index
in the back of a book
• An index helps speed up SELECT queries and
WHERE clauses, but it slows down data input,
with UPDATE and INSERT statements. Indexes
can be created or dropped with no effect on the
data.
• Syntax:
• Single-Column Indexes:
– CREATE INDEX index_name ON table_name
(column_name);
• Composite Indexes:
– CREATE INDEX index_name on table_name
(column1, column2);
• Unique Indexes:
– CREATE UNIQUE INDEX index_nameon
table_name (column_name);
• Drop
– DROP INDEX table_name.index_name;
Rename columns
• Alter table table_name rename new
column_name to New column_name
The SQL UNION
• The UNION operator is used to combine the
result-set of two or more SELECT statements.
– Notice that each SELECT statement within the
UNION must have the same number of columns.
– The columns must also have similar data types.
– The columns in each SELECT statement must be
in the same order.
SQL UNION Syntax
• SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
– Note: The UNION operator selects only distinct values
by default. To allow duplicate values, use the ALL
keyword with UNION.
• SQL UNION ALL Syntax
– SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
• Example
– SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
SQL JOIN
– INNER JOIN: Returns all rows when there is at
least one match in BOTH tables
– LEFT JOIN: Return all rows from the left table,
and the matched rows from the right table
– RIGHT JOIN: Return all rows from the right
table, and the matched rows from the left table
– FULL JOIN: Return all rows when there is a
match in ONE of the tables
• The INNER JOIN keyword selects all rows
from both tables as long as there is a match
between the columns in both tables.
• An inner join produces a result set that is
limited to the rows where there is a match in
both tables for what we're looking for.
• SQL INNER JOIN Syntax
– SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
or:
– SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
• Example
– SELECT Customers.CustomerName,
Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
The SQL TOP
• used to fetch a TOP N number or X percent
records from a table.
• Syntax:
– SELECT TOP number|percent column_name(s)
FROM table_name WHERE [condition]
– SELECT TOP 3 * FROM CUSTOMERS;