Calculating Click-Through Rate (CTR)
Given a table of app events with columns event_type, event_date, and app_id, how would you calculate the click-through rate (CTR) for 2022, ensuring you avoid integer division and round the result to two decimal places?
- SELECT ROUND(100.0 * SUM(CASE WHEN event_type = 'click' THEN 1 ELSE 0 END) / SUM(CASE WHEN event_type = 'impression' THEN 1 ELSE 0 END), 2) FROM events WHERE YEAR(event_date) = 2022;
- SELECT ROUND(100 * COUNT(clicks) / COUNT(impressions), 2) FROM events WHERE EXTRACT(YEAR FROM event_date) = 2022;
- SELECT (SUM(clicks) / SUM(impressions)) * 100 FROM events WHERE event_date LIKE '2022%';
- SELECT ROUND(SUM(CASE WHEN event_type = 'click' THEN 1 ELSE 0 END) / SUM(CASE WHEN event_type = 'impression' THEN 1 ELSE 0 END) * 100, 0) FROM events WHERE event_date BETWEEN '2022-01-01' AND '2022-12-31';
- SELECT 100.0 * SUM(clicks) / SUM(impressions) AS ctr FROM events WHERE YEAR(event_date) = 2022;
Using the CASE Statement in Aggregations
Which of the following SQL snippets correctly uses the CASE statement to count the number of 'download' events in a table named events?
- SELECT SUM(CASE WHEN event_type = 'download' THEN 1 ELSE 0 END) FROM events;
- SELECT COUNT(CASE WHEN event_type = 'download' THEN 1 END) FROM events;
- SELECT SUM(IF event_type = 'download' THEN 1 ELSE 0) FROM events;
- SELECT COUNT(*) WHERE event_type = 'download' FROM events;
- SELECT SUM(CASE event_type = 'download' THEN 1 ELSE 0 END) events FROM;
Preventing Integer Division
What technique can be used in SQL to ensure the result of a division operation is not affected by integer division?
- Multiply one operand by 1.0 or use a decimal such as 100.0 in calculations
- Use COUNT() instead of SUM()
- Avoid using GROUP BY in queries
- Use the DISTINCT keyword on all columns
- Add ORDER BY after the division
Optimizing Aggregation Queries
When calculating multiple aggregates like clicks and impressions from the same table, what is a common, more optimized approach?
- Use a single query with SUM and CASE statements for conditional aggregation
- Create two separate tables and join them after aggregation
- Group by every column individually
- Perform sub-queries for each aggregate and UNION the results
- Write a separate SELECT statement for each calculation
Rounding SQL Results
Which SQL function can be used to round the values of calculated fields to a specified number of decimal places?
- ROUND()
- TRUNCATE()
- FLOOR()
- SUM()
- ABS()
Selecting Data for a Specific Year
Which condition would you use in a WHERE clause to only include records from the year 2022 for a column named event_date?
- WHERE YEAR(event_date) = 2022
- WHERE event_date = 2022
- WHERE event_date IN 2022
- WHERE event_date == '2022'
- WHERE event_date CONTAINS '2022'
Counting by Event Type
If you have a table with event_type as either 'click' or 'impression', how would you most efficiently count the number of impressions?
- SELECT COUNT(*) FROM events WHERE event_type = 'impression';
- SELECT COUNT(impression) FROM events;
- SELECT impression FROM events GROUP BY event_type;
- SELECT SUM(impression) FROM events;
- SELECT COUNT(*) WHERE event_type = 'impression' FROM events;
Handling Division by Zero
In calculating a click-through rate (CTR), what should you add to your query to avoid division by zero when the number of impressions might be zero?
- Add a WHERE clause to exclude rows where impressions are zero or use NULLIF or CASE to handle zero denominators
- Use LIMIT 0 in your query
- Use DISTINCT to remove zeros
- CAST clicks as FLOAT
- GROUP BY on the impression column
Calculating Percentages in SQL
Given columns clicks and impressions, which expression correctly calculates the percentage (as a float) of clicks out of impressions?
- 100.0 * clicks / impressions
- clicks / impressions * 100%
- impressions / clicks * 100.0
- SUM(clicks) - impressions
- clicks % impressions
Identifying Optimized SQL Solutions
Given two approaches for calculation: (a) joining two derived tables after separate aggregations, or (b) using SUM with CASE for conditional aggregation in a single query, which is more optimized?
- Using SUM with CASE for conditional aggregation in a single query
- Always joining two derived tables is more optimized
- Using multiple subqueries ensures better optimization
- Breaking up logic into as many subqueries as possible improves speed
- Using ORDER BY before WHERE optimizes the query