0% found this document useful (0 votes)
44 views9 pages

SQL Queries for Database Evaluation

The document contains SQL exercises related to database evaluations, focusing on various SQL commands such as SELECT, GROUP BY, HAVING, and JOIN. It includes multiple queries that analyze employee data, sales, and product information. The exercises are structured to demonstrate the application of SQL functions and aggregations in different scenarios.
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)
44 views9 pages

SQL Queries for Database Evaluation

The document contains SQL exercises related to database evaluations, focusing on various SQL commands such as SELECT, GROUP BY, HAVING, and JOIN. It includes multiple queries that analyze employee data, sales, and product information. The exercises are structured to demonstrate the application of SQL functions and aggregations in different scenarios.
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

NOM: OUATTARA

Prénom: KOUAME HOUSSEINI

Classe: Master 2 SRT HETEC

GMAIL: hassanelejuste96@[Link]

Évaluation de Base de données Orales

Exercice 1 page 34 page 3 a 6 ( groupe BY)


3
SELECT CodP, COUNT(*)
FROM PC
GROUP BY CodP;

4
SELECT CodC, NumC, DATC
FROM Commande
ORDER BY CodC, DATC DESC;

5
SELECT CodC, SUM(COALESCE(MontF, 0))
FROM Facture
GROUP BY CodC;
SELECT CodC, SUM(MontF)
FROM Facture
GROUP BY CodC;

6
SELECT NumC, COUNT(*)
FRON PC
GROUP BY Numc;

II. Page 35 Partie (HAVING )question 1 a 6


1
SELECT CodP, SUM(QteC)
FROM PC
GROUP BY CodP
HAVING SUM(QteC) > 10;

2
SELECT CodP, AVG(QteC)
FROM PC
GROUP BY CodP
HAVING AVG(QteC) > 50;

3
SELECT CodC, COUNT(*)
FROM Commande
GROUP BY CodC
HAVING COUNT(*) > 2;

4
SELECT COUNT(*)
FROM Produit
WHERE PU > (
SELECT AVG(PU) FROM Produit
);

5
SELECT CodC, SUM(MontF)
FROM Facture
WHERE EXTRACT(YEAR FROM DATF) = 2014
GROUP BY CodC
HAVING SUM(MontF) > 1000;

6
SELECT COUNT(*)
FROM PC
WHERE CodP = 2
AND NumC IN (
SELECT NumC
FROM PC
GROUP BY NumC
HAVING COUNT(*) > 10) ;
Page 39 exo 1 a 7
1- SELECT
e.first_name AS prenom,
[Link] AS salaire,
j.job_title AS travail,
d.department_name AS departement
FROM
employees e
JOIN
jobs j ON e.job_id = j.job_id
JOIN
departments d ON e.department_id = d.department_id
ORDER BY
[Link] DESC;

2
SELECT
c.country_name AS pays,
COUNT(e.employee_id) AS nombre_employes,
SUM([Link]) AS masse_salaire
FROM
employees e
JOIN
departments d ON e.department_id = d.department_id
JOIN
locations l ON d.location_id = l.location_id
JOIN
countries c ON l.country_id = c.country_id
JOIN
regions r ON c.region_id = r.region_id
WHERE
r.region_name = 'Europe'
GROUP BY
c.country_name
ORDER BY
masse_salaire DESC;

3
SELECT
d.department_name AS departement,
l.street_address AS adresse,
[Link] AS ville,
COUNT(e.employee_id) AS nombre_employes_commission
FROM
departments d
JOIN
locations l ON d.location_id = l.location_id
JOIN
employees e ON d.department_id = e.department_id
WHERE
e.commission_pct IS NOT NULL
AND e.commission_pct > 0
GROUP BY
d.department_name, l.street_address, [Link]
ORDER BY
nombre_employes_commission DESC;

SELECT
e.first_name AS "Prénom",
e.last_name AS "Nom",
r.region_id AS "N° région",
r.region_name AS "Région",
c.country_name AS "Pays",
[Link] AS "Ville"
FROM
employees e
JOIN departments d ON e.department_id = d.department_id
JOIN locations l ON d.location_id = l.location_id
JOIN countries c ON l.country_id = c.country_id
JOIN regions r ON c.region_id = r.region_id
ORDER BY r.region_name, e.last_name;

5- SELECT
d.department_id AS "N° Dépt",
d.department_name AS "Département",
e.first_name || ' ' || e.last_name AS "Employé",
j.job_title AS "Poste",
[Link] AS "Ville"
FROM
employees e
JOIN departments d ON e.department_id = d.department_id
JOIN jobs j ON e.job_id = j.job_id
JOIN locations l ON d.location_id = l.location_id
JOIN countries c ON l.country_id = c.country_id
WHERE c.country_name = 'France'
ORDER BY d.department_id, e.last_name;

6-

SELECT
j.job_title AS "Poste",
MIN([Link]) AS "Salaire min",
MAX([Link]) AS "Salaire max",
MAX([Link]) - MIN([Link]) AS "Écart salarial",
ROUND(AVG([Link]), 2) AS "Moyenne"
FROM
employees e
JOIN departments d ON e.department_id = d.department_id
JOIN jobs j ON e.job_id = j.job_id
WHERE d.department_name = 'Administration'
GROUP BY j.job_title
ORDER BY "Écart salarial" DESC;
5- SELECT
d.department_id AS "N° Dépt",
d.department_name AS "Département",
e.first_name || ' ' || e.last_name AS "Employé",
j.job_title AS "Poste",
[Link] AS "Ville"
FROM
employees e
JOIN departments d ON e.department_id = d.department_id
JOIN jobs j ON e.job_id = j.job_id
JOIN locations l ON d.location_id = l.location_id
JOIN countries c ON l.country_id = c.country_id
WHERE c.country_name = 'France'
ORDER BY d.department_id, e.last_name;

7- SELECT
e.first_name,
e.last_name,
EXTRACT(YEAR FROM AGE(CURRENT_DATE, e.birth_date)) AS age,
j.job_title,
d.department_name
FROM
employees e
/* mêmes jointures */
WHERE
j.job_title = 'Finance Manager'
AND [Link] = 'Washington'
AND EXTRACT(YEAR FROM AGE(CURRENT_DATE, e.birth_date))
BETWEEN 20 AND 50;

Common questions

Powered by AI

SQL query optimization involves strategies such as indexing, limiting data retrieval to essential elements, and optimizing join operations. For joins, selecting appropriate join types based on dataset size and query intent, as well as ensuring join conditions are indexed, improves performance. With subqueries, utilizing EXISTS or IN clauses effectively, and flattening queries when possible, enhances speed. Efficient querying like "SELECT e.first_name AS "Prénom", e.last_name AS "Nom",...FROM employees e JOIN departments d ON e.department_id = d.department_id..." by structuring joins hierarchically ensures minimal processing overhead and faster data retrieval .

Analyzing employees' ages alongside their positions contributes to workforce planning by helping organizations understand demographic distributions, anticipate retirement trends, and strategize succession planning. It aids in tailoring recruitment and development strategies to ensure optimal skill distribution across age groups. The query "SELECT e.first_name, e.last_name, EXTRACT(YEAR FROM AGE(CURRENT_DATE, e.birth_date)) AS age, j.job_title, d.department_name FROM employees... WHERE j.job_title = 'Finance Manager' AND l.city = 'Washington'..." exemplifies how understanding the age range of key roles like Finance Managers informs strategies on maintaining a balanced and sustainable workforce .

The HAVING clause is used in SQL to filter results based on aggregate computations performed in a GROUP BY clause. Unlike the WHERE clause, which filters rows before any grouping occurs, the HAVING clause filters groups after the data has been aggregated. For example, "SELECT CodP, SUM(QteC) FROM PC GROUP BY CodP HAVING SUM(QteC) > 10" uses HAVING to filter groups where the sum of quantities "QteC" exceeds 10 .

Ordering results in SQL queries is important as it provides structured data visualization, facilitating the identification of trends, outliers, and patterns. It enhances user interpretation, helps in data validation, and improves decision-making processes. For instance, ordering employees by salary as seen in "SELECT e.first_name AS prenom, e.salary AS salaire,...ORDER BY e.salary DESC" allows companies to easily identify top earners, evaluate pay scales, and ensure competitive compensation frameworks, impacting strategic payroll and talent retention decisions .

Grouping employees by geographical region can provide valuable insights into regional workforce distributions, salary expenses, and resource allocation. It helps in understanding regional performance differences, optimizing regional strategies, and evaluating satisfaction or productivity trends based on location. For example, the query "SELECT c.country_name AS pays, COUNT(e.employee_id) AS nombre_employes, SUM(e.salary) AS masse_salaire FROM employees... WHERE r.region_name = 'Europe' GROUP BY c.country_name..." shows how effectively salary masses and employee counts are broken down by country, aiding in regional budget and workforce planning .

Subqueries within the WHERE clause are used to perform operations that depend on data retrieval from other parts of the database, enhancing query functionality and enabling complex condition evaluations. They provide a way to implement nested filters and checks based on dynamic data sets, making queries more robust and accurate. For instance, using "SELECT COUNT(*) FROM PC WHERE CodP = 2 AND NumC IN ( SELECT NumC FROM PC GROUP BY NumC HAVING COUNT(*) > 10)" filters records based on conditional subsets derived from another query's results, illustrating their utility in operationalizing complex data conditions .

Aggregate functions like COUNT and SUM are critical in data analysis as they allow for the summarization and quantification of data. COUNT helps in understanding the number of occurrences or entities, such as products sold or commands issued, which is crucial for workload and resource planning. SUM provides the total amounts, such as revenue or costs, essential for financial analysis and budget allocation. An example query "SELECT CodC, COUNT(*) FROM Commande GROUP BY CodC HAVING COUNT(*) > 2" counts entries exceeding a threshold for performance assessments .

JOIN clauses are advantageous as they allow for the combination of data from multiple tables based on related columns, providing a comprehensive view and enabling complex data analysis. They help in efficiently retrieving related data without redundancy, optimizing database queries for clearer insights. For instance, "SELECT e.first_name AS prenom, e.salary AS salaire, j.job_title AS travail, d.department_name AS departement FROM employees e JOIN jobs j ON e.job_id = j.job_id JOIN departments d ON e.department_id = d.department_id..." joins employee, job, and department tables to present integrated information about employee roles and compensation .

Analyzing salary distributions across job titles in the 'Administration' department reveals salary ranges, disparities, and averages which can inform discussions about equity and positions that require budget adjustments. The query "SELECT j.job_title AS 'Poste', MIN(e.salary) AS 'Salaire min', MAX(e.salary) AS 'Salaire max', (MAX(e.salary) - MIN(e.salary)) AS 'Écart salarial', ROUND(AVG(e.salary), 2) AS 'Moyenne' FROM employees e...GROUP BY j.job_title ORDER BY 'Écart salarial' DESC" allows one to understand minimum, maximum, average salaries, and pay disparities for different positions, which are crucial for strategic salary planning and ensuring competitive employee compensation .

Employing aggregate functions within a HAVING clause allows businesses to apply post-aggregation filters, leading to refined operational insights such as performance benchmarks and exception management. This can direct resource allocation, operational efficiency improvements, and target-setting based on comprehensively analyzed metrics. For example, using "SELECT CodC, SUM(MontF) FROM Facture WHERE EXTRACT(YEAR FROM DATF) = 2014 GROUP BY CodC HAVING SUM(MontF) > 1000" allows identifying high-value clients or orders, crucial for cost-management and client strategy .

You might also like