Results for Programming
120 years of Olympic history | SQL Project | MySQL Workbench
SQL Workbench

In this SQL project, I worked on a 120-year Olympic history dataset, where I tried to find some insight by writing SQL queries. I used MySQL Workbench to analyze the dataset for this project. I have analyzed the dataset in the form of questions and answers by writing SQL queries where i used joins, common table expression, subqueries, window function, etc.

This dataset contains 2 tables, in CSV format:
  • The Athlete Events table contains over 270,000 Olympic performances across history
  • Each record represents an individual athlete competing in an individual event
  • Records contain details about the athlete (ID, sex, name, age, height, weight, country) and the event (games, year, city, sport, event, medal)
  • The NOC Region table serves as a lookup table with “NOC” as the primary key
  • Each record represents one country according to the National Olympic Committee.

Loading dataset to MySQL Workbench Database:

athlete_events table:


noc_regions table:



SQL Queries:
I am writing SQL Queries using this data. For each of these queries, you would find the problem statement, SQL Queries and then the screen shot of the output(some screen shot contains few lines of output).


1. How many olympics games have been held?
Problem Statement: Write a SQL query to find the total no of Olympic Games held as per the dataset.

select count(distinct(Games)) as olympics_games
from athlete_events;

Output:



2. List down all Olympics games held so far.
Problem Statement: Write a SQL query to list down all the Olympic Games held so far.

select distinct year, season, city
from athlete_events
order by year;

Output:



3. Mention the total no of nations who participated in each olympics game?
Problem Statement: SQL query to fetch total no of countries participated in each olympic games.

with all_countries as
    (select games, nr.region
    from athlete_events oh
    join noc_regions nr ON nr.noc = oh.noc
    group by games, nr.region)
select games, count(1) as total_countries
from all_countries
group by games
order by games;

Output:



4. Which year saw the highest and lowest no of countries participating in Olympics
Problem Statement: Write a SQL query to return the Olympic Games which had the highest participating countries and the lowest participating countries.

with all_countries as
    (select games, nr.region
    from athlete_events oh
    join noc_regions nr ON nr.noc=oh.noc
    group by games, nr.region),
tot_countries as
    (select games, count(1) as total_countries
    from all_countries
    group by games)
select distinct
concat(first_value(games) over(order by total_countries)
, ' - '
, first_value(total_countries) over(order by total_countries)) as Lowest_Countries,
concat(first_value(games) over(order by total_countries desc)
, ' - '
, first_value(total_countries) over(order by total_countries desc)) as Highest_Countries
from tot_countries
order by 1;

Output:



5. Which nation has participated in all of the olympic games?
Problem Statement: SQL query to return the list of countries who have been part of every Olympics games.

with tot_games as
    (select count(distinct games) as total_games
    from athlete_events),
countries as
    (select games, nr.region as country
    from athlete_events oh
    join noc_regions nr ON nr.noc=oh.noc
    group by games, nr.region),
countries_participated as
    (select country, count(1) as total_participated_games
    from countries
    group by country)
select cp.*
from countries_participated cp
join tot_games tg on tg.total_games = cp.total_participated_games
order by 1;

Output:



6. Identify the sport which was played in all summer olympics.
Problem Statement: SQL query to fetch the list of all sports which have been part of every olympics.

with tot_games as
    (select count(distinct(games)) as total_games
    from athlete_events where season = 'Summer'),
sports_played as
    (select games, sport
    from athlete_events
    group by games, sport),
game_participated as
    (select sport, count(1) as total_participated_games
    from sports_played
    group by sport)
select cp.*
from game_participated cp
join tot_games tg on tg.total_games = cp.total_participated_games
order by 1;

Output:



7. Which Sports were just played only once in the olympics.
Problem Statement: Using SQL query, Identify the sport which were just played once in all of olympics.

with t1 as
    (select distinct games, sport
    from athlete_events),
t2 as
    (select sport, count(1) as no_of_games
    from t1
    group by sport)
select t2.*, t1.games
from t2
join t1 on t1.sport = t2.sport
where t2.no_of_games = 1
order by t1.sport;

Output:



8. Fetch the total no of sports played in each olympic games.
Problem Statement: Write SQL query to fetch the total no of sports played in each olympics.

select Games, count(distinct(Sport)) as total_sports
from athlete_events
group by Games
order by total_sports desc, Games;

Output:



9. Fetch oldest athletes to win a gold medal
Problem Statement: SQL Query to fetch the details of the oldest athletes to win a gold medal at the olympics.

select * from athlete_events
where Medal = 'Gold' and
Age = (select max(Age) from athlete_events where Medal = 'Gold');

Output:



10. Fetch the top 5 athletes who have won the most gold medals.
Problem Statement: SQL query to fetch the top 5 athletes who have won the most gold medals.

with m1 as(
    select Name, Team, count(Medal) as tot
    from athlete_events
    where Medal ='Gold'
    group by Name, Team
    order by tot desc),
m2 as
    (select Name, Team, tot, dense_rank() over(order by tot desc) as rnk
    from m1)
select name, team, tot
from m2
where rnk <= 5;

Output:



11. Fetch the top 5 athletes who have won the most medals (gold/silver/bronze).
Problem Statement: SQL Query to fetch the top 5 athletes who have won the most medals (Medals include gold, silver and bronze).

with m1 as(
    select Name, Team, count(Medal) as tot
    from athlete_events
    where Medal <> 'NA'
    group by Name, Team
    order by tot desc, Team, Name),
m2 as
    (select Name, Team, tot, dense_rank() over(order by tot desc) as rnk
    from m1)
select name, team, tot
from m2
where rnk <= 5;

Output:



12. Fetch the top 5 most successful countries in olympics. Success is defined by no of medals won.
Problem Statement: Write a SQL query to fetch the top 5 most successful countries in olympics. (Success is defined by no of medals won).

with cte as(
    select r.Region, count(a.Medal) as Total_Medal
    from athlete_events a join noc_regions r
    on a.noc = r.noc
    where a.Medal <> 'NA'
    group by r.Region
    order by Total_Medal desc)
select Region, Total_Medal, row_number() over(order by Total_Medal desc) as rnk
from cte limit 5;

Output:



reference :
https://techtfq.com/blog/practice-writing-sql-queries-using-real-dataset
https://www.mavenanalytics.io/blog/maven-olympics-challenge





Technology Topper Sunday, January 08, 2023
Read more ...
Movie Ratings Analysis | Python Project | Pandas, Seaborn, Matplotlib

In this Python Data Analysis Project, I worked on a dataset that contains the different genres of movies, ratings received by the expert and audience, year of release, and budget in millions. While working on this project, I used the Pandas, Seaborn, and Matplotlib python libraries. By using these libraries, I built different types of graphs and charts, which helps us make the analysis easier. In this project, I have analyzed which movie genres are popular among the audience based on their ratings. We can suggest which genre would be best if the producer or director wanted to make a movie at the end of this project.

Python Code
        Most of the movies belong to the action, comedy, and drama genres, which shows that audiences are more interested in this type of movie. Movies with very high audience and critic ratings are considered to be highly liked. That happens mostly for action, drama, and thrillers. By analyzing this dataset, domain experts can make their decisions for upcoming movies that are popular among the audience as per their ratings. So, if a producer or director wants to make a film, the above-mentioned genre will be most preferred in order to get the best response from the audience and earn the most profit from that film.
Movie Ratings Analysis | Python Project




Technology Topper Wednesday, December 28, 2022
Read more ...

SET Operators in SQL | UNION, UNION ALL, INTERSECT, MINUS

Set operators are specialized types of operators that are used to combine the results of two or more queries. These operators are used to extract the desired results from the table data that is stored in the table. The set operators and SQL joins have a similar look, but they differ significantly. SQL joins combine the columns from different tables, whereas SQL operators combine rows from different queries. Though both concepts are used to combine data from multiple tables, joins combine columns from separate tables, whereas set operations combine rows from separate tables.

When using set operators in SQL, you must follow certain rules. The following are some of these:
  • The number of columns in the both the SELECT statement must be the same.
  • The order of columns must be in the same order.
  • The selected columns must have the same data type.

Types of Set Operations 
There are different types of set operators that are mentioned below:
  • UNION
  • UNION ALL
  • INTERSECT
  • MINUS
SET Operators in SQL


To understand the above set operators lets consider a two tables. We will perform all set operation on above mentioned table.
table1table2
AA
BB
CE
DF

1. UNION 
  • UNION combines & return the distinct result of two or more SELECT statements.
  • Output sorted by default
  • Not desired from performance aspect since this involves duplicate removal & sorting.
Syntax:
SELECT * FROM table1
UNION
SELECT * FROM table2;

Output:
output
A
B
C
D
E
F


2. UNIONALL 
  • UNIONALL return all the rows from two or more SELECT statements.
  • Output not sorted by default
  • Wont remove duplicate values
  • Desired from performance aspect since no duplicate removal & sorting are performed.
Syntax:
SELECT * FROM table1
UNIONALL
SELECT * FROM table2;

Output:
output
A
B
C
D
A
B
E
F


3. INTERSECT 
  • INTERSECT return all the common rows from two or more SELECT statements.
  • Output is sorted by default.
  • Remove the duplicate records.
Syntax:
SELECT * FROM table1
INTERSECT
SELECT * FROM table2;

Output:
output
A
B


4. MINUS 
  • MINUS return all the record from one table excluding records from other table.
  • Output is sorted by default.
  • Remove the duplicate records.
Syntax:
SELECT * FROM table1
MINUS
SELECT * FROM table2;

SELECT * FROM table2
MINUS
SELECT * FROM table1;

Output:
output
C
D

output
E
F



Technology Topper Monday, December 26, 2022
Read more ...

SQL Operators

An operator is a reserved word or a character used primarily in an SQL statement WHERE clause to perform operation(s), such as comparisons and arithmetic operations. These Operators are used to specify conditions in an SQL statement and to serve as conjunctions for multiple conditions in a statement.

SQL operators are used for filtering the table's data by a specific condition in the SQL statement.

Types of Operator
SQL operators are categorized in the following categories:
  • SQL Arithmetic Operators
  • SQL Comparison Operators
  • SQL Logical Operators
SQL Operators


Precedence of SQL Operator
The precedence of SQL operators is the sequence in which the SQL evaluates the different operators in the same expression. Structured Query Language evaluates those operators first, which have high precedence.

SQL Operator Symbols

Operators

**

Exponentiation operator

=+, -

Identity operator, Negation operator

*, /

Multiplication operator, Division operator

=+, -, ||

Addition (plus) operator, subtraction (minus) operator, String Concatenation operator

=, !=, <, >, <=, >=, IS NULL, LIKE, BETWEEN, IN

Comparison Operators

NOT

Logical negation operator

&& or AND

Conjunction operator

OR

Inclusion operator



1. Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations such as addition, subtraction, division, and multiplication. These operators usually accept numeric operands.

Operator

Operation

Description

+

Addition

Adds operands on either side of the operator

-

Subtraction

Subtracts the right-hand operand from the left-hand operand

*

Multiplication

Multiplies the values on each side

/

Division

Divides left-hand operand by right-hand operand

%

Modulus

Divides left-hand operand by right-hand operand and returns the remainder



2. Comparison Operators
The comparison operators in SQL compare two different sets of data from SQL tables and check whether they are the same, greater, or lesser. It checks whether one expression is identical to another. Comparison operators are generally used in the WHERE clause of a SQL query. The result of a comparison operation may be true, false, or unknown. When one or both of the expressions are NULL, then the operator returns UNKNOWN. These operators could be used on all types of expressions except those that contain text, ntext or images.

Operator

Operation

Description

=

Equal to

Checks if both operands have equal value, if yes, then returns TRUE

>

Greater than

Checks if the value of the left-hand operand is greater than the right-hand operand or not

<

Less than

Returns TRUE if the value of the left-hand operand is less than the value of the right-hand operand

>=

Greater than or equal to

It checks if the value of the left-hand operand is greater than or equal to the value of the right-hand operand, if yes, then returns TRUE

<=

Less than or equal to

Examines if the value of the left-hand operator is less than or equal to the right-hand operand

<> or !=

Not equal to

Checks if values on either side of the operator are equal or not. Returns TRUE if values are not equal

!>

Not greater than

Used to check if the left-hand operator’s value is not greater than or equal to the right-hand operator’s value

!<

Not less than

Used to check if the left-hand operator’s value is not less than or equal to the right-hand operator’s value



3. Logical Operators
The logical operators in SQL perform the Boolean operations, which give two results: true and false. Logical operators take two expressions as operands and return TRUE or FALSE as output. While working with complex SQL statements and queries, comparison operators come in handy, and these operators work in the same way as logic gates do.

Operator

Operation

Description

=

Equal to

Checks if both operands have equal value, if yes, then returns TRUE

>

Greater than

Checks if the value of the left-hand operand is greater than the right-hand operand or not

<

Less than

Returns TRUE if the value of the left-hand operand is less than the value of the right-hand operand

>=

Greater than or equal to

It checks if the value of the left-hand operand is greater than or equal to the value of the right-hand operand, if yes, then returns TRUE

<=

Less than or equal to

Examines if the value of the left-hand operator is less than or equal to the right-hand operand

<> or !=

Not equal to

Checks if values on either side of the operator are equal or not. Returns TRUE if values are not equal

!>

Not greater than

Used to check if the left-hand operator’s value is not greater than or equal to the right-hand operator’s value

!<

Not less than

Used to check if the left-hand operator’s value is not less than or equal to the right-hand operator’s value




Technology Topper Saturday, December 10, 2022
Read more ...