From 2058249c9763111a87a6c91e1540091ace1cdc41 Mon Sep 17 00:00:00 2001 From: Uzziah Ngogela <127846165+UzMatic@users.noreply.github.com> Date: Thu, 21 Nov 2024 10:28:35 +0200 Subject: [PATCH] Update questions.sql --- SQL Deep Dive/Date Filtering/questions.sql | 38 +++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/SQL Deep Dive/Date Filtering/questions.sql b/SQL Deep Dive/Date Filtering/questions.sql index 64aca98..1616801 100644 --- a/SQL Deep Dive/Date Filtering/questions.sql +++ b/SQL Deep Dive/Date Filtering/questions.sql @@ -3,38 +3,60 @@ * Table: employees * Question: Get me all the employees above 60, use the appropriate date functions */ - -SELECT * FROM employees; +SELECT * +FROM employees +WHERE EXTRACT(YEAR FROM AGE(birth_date)) > 60; /* * DB: Employees * Table: employees * Question: How many employees where hired in February? */ - -SELECT * FROM employees; +SELECT count( * ) AS Feb_Employees +FROM employees +WHERE EXTRACT(MONTH FROM hire_date) = 2; /* * DB: Employees * Table: employees * Question: How many employees were born in november? */ - -SELECT * FROM employees; +SELECT count( * ) AS Birth_Month +FROM employees +WHERE EXTRACT(MONTH FROM birth_date) = 11; /* * DB: Employees * Table: employees * Question: Who is the oldest employee? (Use the analytical function MAX) */ +SELECT * +FROM employees +WHERE AGE(birth_date) = ( + SELECT MAX(AGE(birth_date)) + FROM employees +) +LIMIT 1; + +The oldest employee is = jouni pocchiola + -SELECT * FROM employees; /* * DB: Store * Table: orders * Question: How many orders were made in January 2004? */ +SELECT count(*) +FROM orders +WHERE orderdate BETWEEN '2004-01-01' AND '2004-01-31'; +1000 orders were made in january + +The way to do it with the date functions is -SELECT * FROM orders; +SELECT COUNT(*) AS Order_Count +FROM Orders +WHERE EXTRACT(Month FROM orderdate) = 1 +AND EXTRACT(YEAR FROM orderdate) = 2004; +and the result is still 1000 orders