SQL Lab No.03 SQL Aggregate Functions: Department Department Department
SQL Lab No.03 SQL Aggregate Functions: Department Department Department
SQL Lab No.03 SQL Aggregate Functions: Department Department Department
03
Department Table
Employee
Example:
SQL COUNT(*) Syntax SELECT COUNT(*) as TotalRecords
The COUNT(*) function returns the number FROM employee;
of records in a table SELECT COUNT(*) as TotalRecords
FROM employee
SELECT COUNT(*) FROM table_name; Where salary>=50000;
Example:
SQL COUNT(column_name) Syntax SELECT COUNT(commission) as Total
FROM employee;
The COUNT(column_name) function returns
the number of values (NULL values will
not be counted) of the specified column:
SELECT Count(column_name) FROM table_name
Example:
The MAX() function returns the largest SELECT Max(salary) as MaximumSalary
value of the selected column. FROM employee;
Syntax:
SELECT MAX(column_name) FROM table_name;
Example:
The MIN() function returns the largest 1-SELECT Min(salary) as MinimumSalary
value of the selected column. FROM employee;
Syntax: 2-SELECT Min(salary) as MinimumSalary,
Min(commission) as Minimumcommission
SELECT MIN(column_name) FROM table_name; FROM employee;
1-SELECT SUM(salary) as summationSalary
The SUM() function returns the total sum FROM employee;
of a numeric column. 2-SELECT SUM(commission) as
Syntax: summofCommission
SELECT SUM(column_name) FROM table_name; FROM employee;
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
Sub Queries:
In Oracle, a sub query is a query within a query. You can create sub queries
within your SQL statements. These sub queries can reside in the WHERE clause,
the FROM clause, or the SELECT clause.
WHERE clause
Most often, the sub query will be found in the WHERE clause. These sub
queries are also called nested sub queries.
SELECT *
FROM all_tables tabs
WHERE tabs.table_name IN (SELECT cols.table_name
FROM all_tab_columns cols
WHERE cols.column_name = 'SUPPLIER_ID');
Select emp_first_name,emp_last_name
From employee
Home Task: Write a query to find out the 2nd highest salary from employee
table.
Note: Quiz will be held in next class relating to SQL. (All topics)