Skip to content

Instantly share code, notes, and snippets.

@kruszczynski
Last active May 10, 2021 11:50
Show Gist options
  • Save kruszczynski/ed8f334b62e1f96a956cd7775ad5929f to your computer and use it in GitHub Desktop.
Save kruszczynski/ed8f334b62e1f96a956cd7775ad5929f to your computer and use it in GitHub Desktop.

SQL

Schema

clients
 - id
 - email

client_plans
 - id
 - client_id
 - plan_id
 - date_started
 - date_ended

plans
 - id
 - name
 - price

Sample data

clients:
 1,"russ@rainforestqa.com"
 
client_plans:
 1,1,7,2013-01-01,2013-01-03
 2,1,8,2013-01-03,2013-01-06
 3,1,7,2013-01-07,null

plans:
 7,"small",$10
 8,"large",$50

clients with a client_plans record with a null date_ended are still on that plan

  1. Give me all the ids of clients who at some point (now or in the past) were on plan_id 7
  2. Give me the email addresses of clients currently on plan_id 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment