0% found this document useful (0 votes)
31 views73 pages

05-Agrupamiento Prof

The document discusses various SQL aggregate functions such as COUNT, SUM, AVG, MAX, MIN, and VARIANCE that can be used to retrieve aggregated data from tables, including how to use these functions with GROUP BY to group and filter results. It also covers UNION, INTERSECT, and MINUS operators that allow combining result sets from multiple queries.

Uploaded by

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

05-Agrupamiento Prof

The document discusses various SQL aggregate functions such as COUNT, SUM, AVG, MAX, MIN, and VARIANCE that can be used to retrieve aggregated data from tables, including how to use these functions with GROUP BY to group and filter results. It also covers UNION, INTERSECT, and MINUS operators that allow combining result sets from multiple queries.

Uploaded by

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















• SELECT
MAX(salary)
FROM
employees;


• SELECT
MAX(salary)
FROM
employees;




SELECT column, group_function(column),


..
FROM table
WHERE condition
GROUP BY column;




SELECT last_name, first_name


FROM employees
WHERE salary = MIN(salary);

SELECT MIN(life_expect_at_birth)
AS "Lowest Life Exp"
FROM wf_countries;
SELECT MIN(country_name)
FROM wf_countries;
SELECT MIN(hire_date)
FROM employees;

SELECT MAX(life_expect_at_birth)
AS "Highest Life Exp"
FROM wf_countries;
SELECT MAX(country_name)
FROM wf_countries
SELECT MAX(hire_date)
FROM employees;

SELECT SUM(area)
FROM wf_countries
WHERE region_id = 29;
SELECT SUM(salary)
FROM employees
WHERE department_id = 90;

SELECT AVG(area)
FROM wf_countries
WHERE region_id = 29;
SELECT ROUND(AVG(salary), 2)
FROM employees
WHERE department_id = 90;

SELECT
ROUND(VARIANCE(life_expect_at_birth),4)
FROM wf_countries;
SELECT
ROUND(STDDEV(life_expect_at_birth), 4)
FROM wf_countries;

SELECT AVG(commission_pct)
FROM employees;

SELECT MAX(salary), MIN(salary), MIN(employee_id)


FROM employees
WHERE department_id = 60;













SELECT COUNT(job_id)
FROM employees;
• SELECT commission_pct
FROM employees;

• SELECT COUNT(commission_pct)
FROM employees;



SELECT COUNT(*)
FROM employees
WHERE hire_date < '01-Jan-1996';

SELECT COUNT(*)
FROM employees
WHERE hire_date < '01-Jan-1996';


SELECT job_id
FROM employees;

SELECT DISTINCT job_id


FROM employees;
• SELECT DISTINCT job_id,
department_id
FROM employees;


• SELECT DISTINCT job_id,
department_id
FROM employees;
SELECT SUM(salary)
• FROM employees
WHERE department_id = 90;


….

SELECT SUM(DISTINCT salary)


• FROM employees
WHERE department_id = 90;

….

SELECT COUNT (DISTINCT job_id) SELECT COUNT (DISTINCT salary)


FROM employees; FROM employees;


SELECT AVG(NVL(customer_orders, 0))


SELECT AVG(commission_pct)
FROM employees;

SELECT AVG(NVL(commission_pct, 0))


FROM employees;



SELECT AVG(height) FROM students WHERE year_in_school = 10;


SELECT AVG(height) FROM students WHERE year_in_school = 11;
SELECT AVG(height) FROM students WHERE year_in_school = 12;



• SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id
ORDER BY department_id;


• SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id
ORDER BY department_id;


SELECT MAX(salary)
FROM employees
GROUP BY department_id;

SELECT department_id, MAX(salary)


FROM employees
GROUP BY department_id;

-


SELECT job_id, last_name, AVG(salary)
FROM employees
GROUP BY job_id;
• SELECT COUNT(country_name), region_id
FROM wf_countries
GROUP BY region_id
ORDER BY region_id;



SELECT COUNT(*), region_id
FROM wf_countries
GROUP BY region_id
ORDER BY region_id;


SELECT department_id, MAX(salary)


FROM employees
WHERE last_name != 'King'
GROUP BY department_id;

-


SELECT region_id, ROUND(AVG(population)) AS population
FROM wf_countries
GROUP BY region_id
ORDER BY region_id;


SELECT country_id, COUNT(language_id) AS "Number of languages"
FROM wf_spoken_languages
GROUP BY country_id;



• SELECT department_id, job_id, count(*)
FROM employees
WHERE department_id > 40
GROUP BY department_id, job_id;


• SELECT department_id, job_id, count(*)
FROM employees
WHERE department_id > 40
GROUP BY department_id, job_id;

SELECT max(avg(salary))
FROM employees
GROUP by department_id;




SELECT department_id, MAX(salary)
FROM employees
WHERE COUNT(*) > 1
GROUP BY department_id;


SELECT department_id,MAX(salary)
FROM employees
GROUP BY department_id
HAVING COUNT(*)>1
ORDER BY department_id;

SELECT region_id,
ROUND(AVG(population))
FROM wf_countries
GROUP BY region_id
HAVING MIN(population)>300000
ORDER BY region_id;








SELECT a_id
FROM a
UNION
SELECT b_id
FROM b;


SELECT a_id
FROM a
UNION ALL
SELECT b_id
FROM b;


SELECT a_id
FROM a
INTERSECT
SELECT b_id
FROM b;


SELECT a_id
FROM a
MINUS
SELECT b_id
FROM b;





SELECT hire_date, employee_id, job_id
FROM employees
UNION
SELECT TO_DATE(NULL),employee_id, job_id
FROM job_history;



SELECT hire_date, employee_id, job_id


FROM employees
UNION
SELECT TO_DATE(NULL),employee_id, job_id
FROM job_history
ORDER BY employee_id;
SELECT hire_date, employee_id, job_id
FROM employees
UNION
SELECT TO_DATE(NULL),employee_id, job_id
FROM job_history
ORDER BY employee_id;

SELECT hire_date, employee_id, TO_DATE(null) start_date,


TO_DATE(null) end_date, job_id, department_id
FROM employees
UNION
SELECT TO_DATE(null), employee_id, start_date, end_date, job_id,
department_id
FROM job_history
ORDER BY employee_id;

You might also like