Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added DataSet_project.zip
Binary file not shown.
83 changes: 83 additions & 0 deletions SQL-part-1-Studio.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

--Question 1: Write a query of the `books` table that returns the top 100 results and includes `book_id`, `authors`, `title`, and `average_rating`. Use an alias for at least one column and sort the result set in descending order of rating. What is the number one book?
SELECT TOP 100
book_id,
authors,
title AS book_title, -- alias for title
average_rating
FROM [BooksDB].[dbo].[books]
ORDER BY average_rating DESC;


--Question 2: Write a query to find the least popular book.

SELECT TOP 1 book_id, title, authors, average_rating
FROM [BooksDB].[dbo].[books]
ORDER BY average_rating ASC;

--Question 3: Which tag is the most popular?

SELECT TOP 1
tag_id, "count"
FROM [BooksDB].[dbo].[book_tags]
ORDER BY "count" DESC;

--Question 4: What is the name of the most popular tag?

SELECT *
FROM BooksDB.dbo.tags
WHERE tag_id = (

SELECT TOP (1) tag_id

FROM BooksDB.dbo.book_tags

ORDER BY "count" DESC)


--Question 5: How many books where released in the first decade of 2000?

SELECT COUNT(*) AS books_2000s
FROM [BooksDB].[dbo].[books]
WHERE original_publication_year BETWEEN 2000 AND 2009;

--Question 6: How many book titles contain the word, "happy"?

SELECT COUNT(*) AS happy_books
FROM [BooksDB].[dbo].[books]
WHERE title LIKE '%happy%';

--Question 7: List the books from the top 3 authors from Question 1.
-- If there is more than one author just use the first one. Sort the title alphabetically by `author` and then by `average_rating`, best rated to lowest. Does this order matter in sorting?

SELECT authors, title, average_rating
FROM BooksDB.dbo.books
WHERE authors = 'j.k. rowling' OR authors = 'bill watterson' OR authors = 'brandon sanderson'
ORDER BY title ASC, average_rating DESC

--Question 8: Write a query that returns the number of authors whose first name is between rock and roll.
SELECT COUNT(*) AS authors_between_rock_roll
FROM [BooksDB].[dbo].[books]
WHERE authors BETWEEN 'rock' AND 'roll';

-- Question: Click here to add a questions that you want to answer and write a query to find
the answer!

--Question-Returns the top 100 books sorted by average_rating (highest first).
SELECT TOP 100
book_id,
authors,
title AS book_title,
average_rating
FROM [BooksDB].[dbo].[books]
ORDER BY average_rating DESC;

--question-Books containing the word "happy"

SELECT COUNT(*) AS happy_books
FROM [BooksDB].[dbo].[books]
WHERE title LIKE '%happy%';




120 changes: 120 additions & 0 deletions SQL-part-1-exercises.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@

--A. The books table

--Question 1 Select the top 1000 rows from the books table.make sure to use the correct syntax for your SQL database.
SELECT Top 1000 *
FROM [BooksDB].[dbo].[books]

--Question 2 count the number of Titles .Are there 1000 titles as promised by the dataset?

SELECT COUNT(Title) AS Number_of_Titles
FROM [BooksDB].[dbo].[books]

--Question 3 Count the number of books where the original_publication_year is earlier than 1800?

SELECT COUNT(Title) AS Books_Before_1800
FROM [BooksDB].[dbo].[books]
WHERE original_publication_year < 1800

--Question 4 Create the query that display distinct authors from the table?

SELECT DISTINCT authors
FROM [BooksDB].[dbo].[books]
ORDER BY authors

--Question 5 Create the query that display acount of all the books that contains a language_code for english.this could be represented in a table as "en" or "eng".

SELECT COUNT(Title) AS english
FROM [BooksDB].[dbo].[books]
WHERE language_code IN ('en', 'eng')

--Question 6 Create the query that checks how many original titles where written during world war 1 era (1914-1921)?

SELECT COUNT(Title) AS WW1_Era
FROM [BooksDB].[dbo].[books]
WHERE original_publication_year BETWEEN 1914 AND 1921


--B.The Books tags table
--Question 1 Select the top 1000 table items ordered by the tag_id?

SELECT TOP 1000 *
FROM [BooksDB].[dbo].[book_tags]
ORDER BY tag_id

--Question 2 create a query that counts the number of good_books_id grouped by tag_id ?

select COUNT(goodreads_book_id) as good_reads
FROM [BooksDB].[dbo].[book_tags]
GROUP BY tag_id

--Question 3 In the last query ,we created new,unnamed column .use As to create Alias to provide a name of your choice to this column?

select COUNT(goodreads_book_id) as Goodreads_books
FROM [BooksDB].[dbo].[book_tags]
GROUP BY tag_id

--C. The rating table
--Question 1 create a query that selects the top 1000 rows from the ratings table in a descnding order?

SELECT TOP 1000 *
FROM [BooksDB].[dbo].[ratings]
ORDER BY rating DESC

--question 2 Create a query that returns the total number of user that has given a rating less than 2?

select COUNT(rating) as total_ratings
from [BooksDB].[dbo].[ratings]
where rating < 2

--Question 3 Create a query that returns the sum of books that have rating 4 or higher?

select COUNT(rating) as total_ratings
from [BooksDB].[dbo].[ratings]
where rating >= 4


--D. The Tags Table
--Question 1 Create a query that return table items where the tag_name describes book as a mystery?

SELECT *
from [BooksDB].[dbo].[tags]
WHERE tag_name LIKE '%mystery%'

--Questtion 2 run the query below. in your worlds what is returning?

SELECT *
FROM BooksDB.dbo.tags
WHERE tag_name < 'd' AND tag_name >= 'c';

--The to Read table
--Question 1: Create a query that uses the `user_id` to count the total number of books that each user wants to read. Print the results in ascending order by `user_id` under the alias 'Total Books To Read'.

--Hint: You can use both `GROUP BY` and `ORDER BY` to complete this query.

SELECT user_id, COUNT(book_id) AS 'Total Books To Read'
FROM [BooksDB].[dbo].[to_read]
GROUP BY user_id
ORDER BY user_id ASC

--Question 2: Create a query that uses `user_id` to count the total number of books each user wants to read. Have the results sort the table by the total number of `book_ids` in descending order and under the alias 'Total Books To Read'.

--Hint: You can use both `GROUP BY` and `ORDER BY` to complete this query.

SELECT user_id, COUNT(book_id) AS 'Total Books To Read'
FROM [BooksDB].[dbo].[to_read]
GROUP BY user_id
ORDER BY COUNT(book_id) DESC













169 changes: 169 additions & 0 deletions SQL-part-4-demos.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
--1which driver have ever finished first?

SELECT forename, surname
FROM formula_one.dbo.drivers
WHERE driverId IN
(
SELECT DISTINCT driverId
FROM formula_one.dbo.results
WHERE position = '1'
)

--2 How many races has each driver entered?

SELECT d.forename, d.surname,
(
SELECT COUNT(*)
FROM formula_one.dbo.results AS r
WHERE r.driverId = d.driverId
) AS race_count
FROM formula_one.dbo.drivers AS d

--Which driver have entered at least 50 competitions?

SELECT
d.forename, d.surname
FROM formula_one.dbo.drivers AS d
WHERE
(
SELECT COUNT(*)
FROM formula_one.dbo.results AS r
WHERE r.driverId = d.driverId
) >= 50

--which driver have finished first at least once? CTE version
WITH Winning_driverIDs AS(
SELECT DISTINCT driverId
FROM formula_one.dbo.results
WHERE position = '1'

)
SELECT d.forename, d.surname
FROM formula_one.dbo.drivers AS d
WHERE d.driverId IN (SELECT driverId FROM Winning_driverIDs);

-- for each circuit,in how many years has it hosted at least one race?
--count of distinct year values for each circuit id, from race table
--information about each circuit itself,like the circuit name,which comes from the circuit table

WITH circuit_years AS
(
Select circuitId,COUNT(DISTINCT year) AS num_years_hosted
from formula_one.dbo.races
GROUP BY circuitId
)
SELECT cir.name AS circuit_name,
cy.num_years_hosted
FROM formula_one.dbo.circuits AS cir
INNER JOIN circuit_years AS cy
ON cir.circuitId = cy.circuitId
ORDER BY cy.num_years_hosted DESC;

-- which drivers have scored points in more than 5 different races?
--need: driver table, results

WITH driver_points AS ( SELECT
driverId,
COUNT(resultId) AS races_with_points
FROM formula_one.dbo.results
WHERE points > 0
GROUP BY driverId
)
SELECT d.forename ,d.surname,
driver_points.races_with_points AS races_with_points
FROM formula_one.dbo.drivers AS d
INNER JOIN driver_points
ON d.driverId = driver_points.driverId
WHERE driver_points.races_with_points > 5
ORDER BY races_with_points;

-- A list of all driver who are either from the UK or from Italy
SELECT forename,surname,nationality
From formula_one.dbo.drivers
WHERE
nationality = 'British'

UNION

SELECT forename,surname,nationality
From formula_one.dbo.drivers
WHERE
nationality = 'Italian'

--list of drivers who have either won a race ,or recorded the fastet lap in a race
--"rank "column in results table is the "rank "of the fastest lap.i.e a rank of 1 means they had the first fastet lap in that race
-- table that pulls drivers who won a race

SELECT
d.forename, d.surname,
'Race Winner' AS achievement_type
FROM formula_one.dbo.drivers AS d
INNER JOIN formula_one.dbo.results AS r
ON d.driverId = r.driverId
WHERE r.position = '1'
UNION
--table that pulls driver who have recorded the fastet lap in a race
SELECT
d.forename, d.surname,
'Fastest Lap' AS achievement_type
FROM formula_one.dbo.drivers AS d
INNER JOIN formula_one.dbo.results AS r
ON d.driverId = r.driverId
WHERE r.rank = '1';

-- create a list of drivers who races in both 2021 and 2022

--create a table of drivers whoc raced in 2021

SELECT DISTINCT d.driverId,
d.forename,
d.surname
FROM formula_one.dbo.drivers AS d
INNER JOIN formula_one.dbo.results AS res
ON d.driverID = res.driverId
INNER JOIN formula_one.dbo.races
ON res.raceId = races.raceId
WHERE races.year=2021

INTERSECT
--table of drivers who races in 2022

SELECT DISTINCT d.driverId,
d.forename,
d.surname
FROM formula_one.dbo.drivers AS d
INNER JOIN formula_one.dbo.results AS res
ON d.driverID = res.driverId
INNER JOIN formula_one.dbo.races
ON res.raceId = races.raceId
WHERE races.year=2022

--which driver raced in 2021 but not 2022

SELECT DISTINCT d.driverId,
d.forename,
d.surname
FROM formula_one.dbo.drivers AS d
INNER JOIN formula_one.dbo.results AS res
ON d.driverID = res.driverId
INNER JOIN formula_one.dbo.races
ON res.raceId = races.raceId
WHERE races.year=2021

EXCEPT
--table of drivers who races in 2022

SELECT DISTINCT d.driverId,
d.forename,
d.surname
FROM formula_one.dbo.drivers AS d
INNER JOIN formula_one.dbo.results AS res
ON d.driverID = res.driverId
INNER JOIN formula_one.dbo.races
ON res.raceId = races.raceId
WHERE races.year=2022





Loading