The document contains SQL query examples for retrieving data from a database. Example 25.1 selects employee details from departments for the year 2017, while Examples 25.2 and 25.3 aggregate sales data by month and product for the years specified. Each example demonstrates different SQL functionalities such as JOINs, GROUP BY, and ORDER BY clauses.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
10 views
Chapter25 Examples
The document contains SQL query examples for retrieving data from a database. Example 25.1 selects employee details from departments for the year 2017, while Examples 25.2 and 25.3 aggregate sales data by month and product for the years specified. Each example demonstrates different SQL functionalities such as JOINs, GROUP BY, and ORDER BY clauses.
FROM department d JOIN employee e ON d.dept_no = e.dept_no JOIN works_on w ON w.emp_no = e.emp_no WHERE YEAR(enter_date) = 2017 ORDER BY dept_name;
Example 25.2
SELECT t.MonthNumberOfYear AS month, t.CalendarYear AS year,
p.ProductKey AS product_id, SUM(f.UnitPrice) AS sum_of_sales, COUNT(f.UnitPrice) AS total_sales FROM DimDate t, DimProduct p, FactInternetSales f WHERE t.DateKey = f.OrderDateKey AND p.ProductKey = f.ProductKey AND CalendarYear = @year GROUP BY t.CalendarYear, t.MonthNumberOfYear, p.ProductKey ORDER BY 1;
Example 25.3
SELECT t.MonthNumberOfYear AS month, t.CalendarYear AS year,
p.ProductKey AS product_id, SUM(f.UnitPrice) AS sum_of_sales, COUNT(f.UnitPrice) AS total_sales FROM DimDate t, DimProduct p, FactInternetSales f WHERE t.DateKey=f.OrderDateKey AND p.ProductKey=f.ProductKey AND CalendarYear = 2013 GROUP BY t.CalendarYear,t.MonthNumberOfYear,p.ProductKey ORDER BY 1;