less than 1 minute read

Problem

problem

My Query

SELECT
    customer_id, COUNT(customer_id) as count_no_trans
FROM
    Visits as v
LEFT JOIN
    Transactions as t
        ON
            v.visit_id = t.visit_id
WHERE
    transaction_id IS NULL
GROUP BY 
    customer_id

Editorial Solution

Approach 1: Removing Records Using NOT IN/EXISTS

SELECT 
  customer_id, 
  COUNT(visit_id) AS count_no_trans 
FROM 
  Visits 
WHERE 
  visit_id NOT IN (
    SELECT 
      visit_id 
    FROM 
      Transactions
  ) 
GROUP BY 
  customer_id

Approach 2: Removing Records Using LEFT JOIN and IS NULL

SELECT 
  customer_id, 
  COUNT(*) AS count_no_trans 
FROM 
  Visits AS v 
  LEFT JOIN Transactions AS t ON v.visit_id = t.visit_id 
WHERE 
  t.visit_id IS NULL 
GROUP BY 
  customer_id

Tags:

Updated: