SQL 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

SQL Views

SQL CREATE VIEW Statement


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.

CREATE VIEW view_name ASSELECT


column_name(s)FROM table_nameWHERE
condition
Studrecord

SID Fname Age


1 Dinku 22
2 Yasin 19
3 Hewan
4 Selam 19
5 daniel 22

StudrecordResult

ModuleCode AssessmentDate Mark Grade SID


100 7/8/2016 99.5 C 3
200 1/6/2016 97 C 3
300 4/16/2016 100 C 2
400 9/22/2016 93 Nyc 1
500 10/22/2016 70 NYC 3
LO2: SQL Functions
SQL Aggregate Functions

•AVG() - Returns the average value


•COUNT() –Returns number of rows
•FIRST() - Returns the first value
•LAST() - Returns the last value
•MAX() - Returns the largest value
•MIN() - Returns the smallest value
•SUM() - Returns the sum
SQL Scalar functions
• UPPER() - Converts a field to upper case
• LCASE() - Converts a field to lower case
• MID() - Extract characters from a text field
• LEN() - Returns the length of a text field
• NOW() - Returns the current system date and
time
• FORMAT() - Formats how a field is to be
displayed
SQL AVG() Function
• SELECT AVG(column_name) FROM
table_name
• Example:
– SELECT AVG(Mark) as AverageMark FROM
Result
– SELECT SName, Mark FROM studrecord where
Mark > (select AVG(Mark) FROM studrecord)
– SELECT SName, Mark FROM studrecord where
Mark < (select AVG(Mark) FROM studrecord)
SQL COUNT() Function
• SELECT COUNT(column_name) FROM
table_name
• SELECT COUNT(*) FROM table_name
• SELECT COUNT(DISTINCT column_name)
FROM table_name
• Example:
– SELECT COUNT(Fname) FROM StudInfo
– SELECT COUNT(*) FROM table_name
– SELECT COUNT(DISTINCT Grade) FROM Result
SQL UPPER/UCASE &LCASE
Function
• SELECT UPPER(column_name) FROM
table_name
• Example:
– SELECT UPPER(LastName) as
LastName,FirstName FROM StudInfo
SQL FIRST, LAST Function
• SELECT FIRST(column_name) FROM
table_name
• Example:
– SELECT FIRST(ExamDate) AS Firsxamdate
FROM result
SQL MAX, MIN Functions
• SELECT MAX(Mark) as HighstMark FROM
Result
SQL SUM Function
• SELECT SUM(column_name) FROM
table_name
LO3: Write SQL statements
that use aggregation and
filtering
2. SQL GROUP BY Statement

• used with aggregate functions to group the


result-set by one or more columns.
• SQL GROUP BY Syntax
– SELECT column_name,
aggregate_function(column_name)FROM
table_name GROUP BY column_name

– 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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy