sqlbeginner10 minutes

Refactor SQL Query for Retrieving Active Users

Optimize and refactor a simple SQL query to improve readability and performance while maintaining the same output.

Challenge prompt

You are given a SQL query that retrieves all users marked as active from a users table. The original query works correctly but is written in a way that can be simplified and made more efficient. Refactor the query to write it more cleanly and optimize the filtering condition. Ensure your refactored query returns the same list of active users with their id, name, and email.

Guidance

  • Focus on simplifying the WHERE clause and removing any unnecessary conditions.
  • Use clear and straightforward syntax for filtering active users.
  • Keep the selected columns and table name unchanged for consistency.

Hints

  • Check if the WHERE clause conditions are redundant or overly complex.
  • Consider if any JOINs or subqueries are unnecessary or can be removed.

Starter code

SELECT id, name, email FROM users WHERE active = 'true' AND active IS NOT NULL;

Expected output

A list of user IDs, names, and emails who are active (active = true).

Core concepts

SQL SELECTWHERE clause filteringQuery simplification

Challenge a Friend

Send this duel to someone else and see if they can solve it.