SQL Query Challenges
Explore writing SQL queries, including data insertion, various clauses, and real-world problem statements.
We'll cover the following...
SQL is one of the most commonly evaluated skills in data interviews. Whether you're analyzing customer behavior or optimizing internal dashboards, you’ll need to write clear, performant queries. In this lesson, we’ll practice using SQL within Python to answer complex real-world prompts. Let’s get started.
Writing a SQL Query
You’re given an Employees table with the following schema:
(id INTEGER, name TEXT, salary INTEGER, department_id INTEGER)
Your task is to write a SQL query that returns employees whose salary is greater than the average salary of their department.
Let’s assume the interviewer has defined a sample function taking care of basic table creation and data insertion for you. You need to fill out the find_high_earners
function (line 38), implementing your SQL query with Python's pandas
and sqlite3
library.
import sqlite3import pandas as pddef initialize_employee_db():# Create an in-memory SQLite databaseconn = sqlite3.connect(':memory:')cursor = conn.cursor()# Create the Employees tablecursor.execute('''CREATE TABLE Employees (id INTEGER PRIMARY KEY,name TEXT,salary REAL,department_id INTEGER)''')# Insert dummy dataemployees_data = [(1, 'John Doe', 60000.00, 1),(2, 'Jane Smith', 75000.00, 1),(3, 'Bob Johnson', 55000.00, 1),(4, 'Alice Brown', 85000.00, 2),(5, 'Charlie Wilson', 95000.00, 2),(6, 'Diana Miller', 65000.00, 2),(7, 'Eva Davis', 120000.00, 3),(8, 'Frank Thomas', 110000.00, 3),(9, 'Grace Taylor', 90000.00, 3)]#Define a query to populate the databasecursor.executemany('INSERT INTO Employees (id, name, salary, department_id) VALUES (?, ?, ?, ?)',employees_data)# Commit the changesconn.commit()return conndef find_high_earners(conn):# TODO - Write your Queryquery = """"""# TODO - execute the query and convert to DataFrameresult_df = pd.read_sql_query(query, conn)#Return the dataframe as a resultreturn result_df
Sample answer
This interview question tests understanding of several different SQL concepts, including: Common Table Expressions (WITH clause), JOINs, window functions and aggregation, and comparison operations. It also tests your knowledge of pandas
and sqlite3
.
Let’s dive into the solution code. The code defines a function find_high_earners
that identifies employees who earn more than the average salary in their respective department. ...