BDMS Assignment 1 SHS
BDMS Assignment 1 SHS
BDMS Assignment 1 SHS
1b: Modify your query to only return customers from Canada or the United States
1b Query:
SELECT FirstName || ' ' || LastName AS FullName,
Company AS CompanyName,
City,
State
FROM Customers
WHERE Country IN ('Canada', 'United States')
ORDER BY City;
1c: Modify your query to only return customers in Canada or the United States
whose last name starts with the letter M.
1c Query:
SELECT FirstName || ' ' || LastName AS FullName,
Company AS CompanyName,
City,
State
FROM Customers
WHERE Country IN ('Canada', 'United States')
AND LastName LIKE 'M%'
ORDER BY City;
2d Query:
SELECT
e.FirstName || ' ' || e.LastName AS Employee,
CASE
WHEN m.FirstName IS NULL THEN e.FirstName || ' ' || e.LastName
ELSE m.FirstName || ' ' || m.LastName
END AS Manager
FROM employees e
LEFT JOIN employees m ON e.ReportsTo = m.EmployeeId
ORDER BY Manager, Employee;
Problem 3: Aggregation
3a: Create a report that that Joins the Albums to Tracks table and returns the title
of each album and the number of tracks it contains.
3a Query:
SELECT
a.Title AS AlbumTitle,
COUNT(t.TrackId) AS NumberOfTracks
FROM
albums a
JOIN
tracks t ON a.AlbumId = t.AlbumId
GROUP BY
a.AlbumId, a.Title
ORDER BY
a.Title;
3b: Modify your query from 3a so that the report only shows albums that have
more than ten tracks.
3b Query:
SELECT
a.Title AS AlbumTitle,
COUNT(t.TrackId) AS NumberOfTracks
FROM
albums a
JOIN
tracks t ON a.AlbumId = t.AlbumId
GROUP BY
a.AlbumId, a.Title
HAVING
COUNT(t.TrackId) > 10
ORDER BY a.Title;
SQL RTF file:
-- Shamanta Hoque Shammi | Assignment 1
-- 1a: Query:
SELECT
FirstName || ' ' || LastName AS FullName,
Company AS CompanyName,
City,
State
FROM Customers
ORDER BY City;
-- 1b Query:
SELECT
FirstName || ' ' || LastName AS FullName,
Company AS CompanyName,
City,
State
FROM Customers
WHERE Country IN ('Canada', 'United States')
ORDER BY City;
-- 1c Query:
SELECT
FirstName || ' ' || LastName AS FullName,
Company AS CompanyName,
City,
State
FROM Customers
WHERE Country IN ('Canada', 'United States')
AND LastName LIKE 'M%'
ORDER BY City;
-- 2a Query:
SELECT
artists.Name AS "Artist Name",
albums.Title AS "Album Title",
tracks.Name AS "Track Names"
FROM
artists
JOIN
albums ON artists.ArtistId = albums.ArtistId
JOIN
tracks ON albums.AlbumId = tracks.AlbumId
ORDER BY artists.Name;
-- 2b Query:
SELECT
artists.Name AS "Artist Name",
albums.Title AS "Album Title",
tracks.Name AS "Track Names"
FROM
artists
JOIN
albums ON artists.ArtistId = albums.ArtistId
JOIN
tracks ON albums.AlbumId = tracks.AlbumId
WHERE
tracks.Name LIKE '%dancing%'
ORDER BY
artists.Name;
-- 2c Query:
SELECT
e.FirstName || ' ' || e.LastName AS Employee,
m.FirstName || ' ' || m.LastName AS Manager
FROM employees e
LEFT JOIN employees m ON e.ReportsTo = m.EmployeeId
ORDER BY Manager, Employee;
-- 2d Query:
SELECT
e.FirstName || ' ' || e.LastName AS Employee,
CASE
WHEN m.FirstName IS NULL THEN e.FirstName || ' ' || e.LastName
ELSE m.FirstName || ' ' || m.LastName
END AS Manager
FROM employees e
LEFT JOIN employees m ON e.ReportsTo = m.EmployeeId
ORDER BY Manager, Employee;
-- Problem 3: Aggregation
-- 3a Query:
SELECT
a.Title AS AlbumTitle,
COUNT(t.TrackId) AS NumberOfTracks
FROM
albums a
JOIN
tracks t ON a.AlbumId = t.AlbumId
GROUP BY
a.AlbumId, a.Title
ORDER BY
a.Title;
-- 3b Query:
SELECT
a.Title AS AlbumTitle,
COUNT(t.TrackId) AS NumberOfTracks
FROM
albums a
JOIN
tracks t ON a.AlbumId = t.AlbumId
GROUP BY
a.AlbumId, a.Title
HAVING
COUNT(t.TrackId) > 10
ORDER BY a.Title;