0% found this document useful (0 votes)
12 views5 pages

SQL Queries for Contest Data Analysis

The document contains three SQL query questions related to contests, submissions, and student salaries. The first question requires a query to summarize contest statistics while excluding contests with no submissions. The second question focuses on identifying unique hackers and the one with the most submissions during a specific contest period, and the third question involves comparing student salaries based on their best friends' offers.

Uploaded by

snsami273
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

SQL Queries for Contest Data Analysis

The document contains three SQL query questions related to contests, submissions, and student salaries. The first question requires a query to summarize contest statistics while excluding contests with no submissions. The second question focuses on identifying unique hackers and the one with the most submissions during a specific contest period, and the third question involves comparing student salaries based on their best friends' offers.

Uploaded by

snsami273
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question : 01

Samantha interviews many candidates from different colleges using coding challenges and
contests. Write a query to print the contest_id, hacker_id, name, and the sums of
total_submissions, total_accepted_submissions, total_views, and total_unique_views for
each contest sorted by contest_id. Exclude the contest from the result if all four sums are 0 .

Note: A specific contest can be used to screen candidates at more than one college, but each
college only holds a screening contest.

Contests Table: The contest_id is the id of the contest, hacker_id is the id of the hacker who
created the contest, and name is the name of the hacker.

Colleges Table: The college_id is the id of the college, and contest_id is the id of the contest
that Samantha used to screen the candidates.

Challenges Table: The challenge_id is the id of the challenge that belongs to one of the
contests whose contest_id Samantha forgot, and college_id is the id of the college where the
challenge was given to candidates.
View_Stats Table: The challenge_id is the id of the challenge, total_views is the number of
times the challenge was viewed by candidates, and total_unique_views is the number of times
the challenge was viewed by unique candidates.

Submission_Stats Table: The challenge_id is the id of the challenge, total_submissions is the


number of submissions for the challenge, and total_accepted_submission is the number of
submissions that achieved full scores.
Question : 02

Julia conducted a 15 days of learning SQL contest. The start date of the contest was March 01,
2025 and the end date was March 15, 2025. Write a query to print the total number of unique
hackers who made at least 1 submission each day (starting on the first day of the contest), and
find the hacker_id and name of the hacker who made the maximum number of submissions
each day. If more than one such hacker has a maximum number of submissions, print the
lowest hacker_id. The query should print this information for each day of the contest, sorted
by the date.

The following tables hold contest data:

Hackers Table: The hacker_id is the id of the hacker, and name is the name of the hacker.
Submissions Table: The submission_date is the date of the submission, submission_id is the
id of the submission, hacker_id is the id of the hacker who made the submission, and score is
the score of the submission.
Question : 03

You are given three tables: Students, Friends and Packages. Students contain two columns: ID
and Name. Friends contains two columns: ID and Friend_ID (ID of the ONLY best friend).
Packages contain two columns: ID and Salary (offered salary in $ thousands per month).

Write a query to output the names of those students whose best friends got offered a higher
salary than them. Names must be ordered by the salary amount offered to the best friends. It is
guaranteed that no two students get the same salary offer.

Students Table:

Friends Table:

Packages Table:

Common questions

Powered by AI

Unique daily session counts, such as `total_unique_views`, offer a more nuanced picture of user engagement by identifying individual users rather than merely tracking activity volume. This method provides insights into genuine user interest and behavior patterns over time, helping to evaluate content or contest appeal and inform future strategies. It assists in distinguishing between repeat visits and broader user base engagement, facilitating targeted improvements in user experience.

Self-referential keys in the Friends table allow for handling hierarchical data, such as relationships or dependencies (e.g., best friends). This key type supports complex queries like finding paths or hierarchical relationships, enabling recursive joins and queries to traverse hierarchical structures. For instance, they facilitate finding all friends related directly or indirectly, enhancing query flexibility and analytical capacities in understanding network or social dynamics within the data.

To retrieve the names of students whose best friends receive a higher salary, perform a `JOIN` operation between the Students and Friends tables to align each student with their best friend. Then join these results with the Packages table to access their respective salaries. Utilize a `WHERE` clause to filter records where the salary associated with the friend's ID is greater than that of the student's ID. Finally, select and order the student names according to the friend's salary.

To exclude contests with all four metric sums equal to zero, you should include a `WHERE` clause in your query that checks the condition `(total_submissions + total_accepted_submissions + total_views + total_unique_views) > 0` after calculating these sums. This ensures that any contest with zero values for all metrics is not included in the results.

Accurate primary keys are crucial in understanding relationships between tables as they uniquely identify records within a table. For example, in the provided document, `contest_id` links the Contests and Colleges tables, signifying which contests are hosted by which colleges. Similarly, `challenge_id` connects Challenges with View_Stats and Submission_Stats, indicating which statistics belong to which contest challenges. Proper primary keys ensure relational integrity by enabling precise joins and data associations across different tables.

To extract the hacker with the highest daily submissions while resolving ties, you would first use a `GROUP BY` clause to group submissions by date and hacker. Within this grouping, use `COUNT()` to calculate the number of submissions per hacker per day. Next, apply a `PARTITION BY` over the daily submissions using `ROW_NUMBER()` ordered by descending count of submissions and ascending hacker_id to resolve ties. Finally, select the rows where `ROW_NUMBER()` equals 1 for selecting the hacker with the most submissions per day.

To maintain SQL query efficiency with high-volume data, use indexing strategies on frequently queried columns such as `contest_id`, `challenge_id`, and `hacker_id`. Additionally, aggregate functions should be preceded by indexed operations to reduce computation time. Implement partitioning on large tables to facilitate quicker data retrieval and minimize the full table scan by utilizing appropriate subqueries or temporary tables to handle complex calculations separately, especially in views and submissions statistics.

Using unique counts in query evaluations, such as `total_unique_views`, helps provide insights into database performance by identifying genuine user engagement. It allows differentiation between repeated actions by the same user and genuine interest from multiple users, offering a more accurate representation of traffic and user interaction with the data.

Constraints in table design, such as primary keys, foreign keys, and uniqueness, play a crucial role in ensuring data retrieval accuracy by enforcing data integrity rules. In the context of the document's tables, constraints ensure that relationships between entities like contests, hackers, and submissions are correctly maintained. This prevents orphaned records, duplicates, or inconsistencies during retrieval, thus providing reliable and accurate data for query outputs.

When calculating daily submission statistics for an extended period, ensure proper date handling by filtering or grouping results per date using a `WHERE` clause or `GROUP BY` clause on the submission date. Consider edge cases such as time zones, submission deadlines, and date formatting. Performance considerations include indexing the date column to speed up retrieval and using appropriate data types for dates to ensure efficient storage and accurate computations.

You might also like