Issue with query results from an SQL join question. Basic Question

Ian Murray

I have two tables, with the below layouts

persons:

person_id,
first_name,
last_name,
gender,
birth_date,
street,
town,
county

likes:

person_id,
food

I'm attempting to "List the names of those who do not like beer" from the above two tables, using just JOINS (I'm not allowed to use Subqueries).

The below code works fine if I change the WHERE clause to equals beer. It results in the two people who like beer.

But when I attempt to change it to those who don't like beer (it's not a real table, everybody likes beer ;)), the issue is it removes the exact row where the food preference is equal to beer. But the person who has other preferences like "pizza" but likes beer is still included.

The query is removing the relevant row for beer but not all the other rows related to this person.

How can I fix this without using a subquery?

SELECT 
    p1.first_name, p1.last_name, l1.food, l1.person_id

FROM
    persons AS p1
    JOIN likes AS l1
        ON p1.person_id = l1.person_id

WHERE l1.food <> "Beer"

For example, Aoife likes beer but also likes pizza. I want Aoife to be removed fully from the query results as she likes beer.

GMB

List the names of those who do not like beer

You can use the LEFT JOIN antipattern:

SELECT p1.first_name, p1.last_name
FROM persons AS p1 
LEFT JOIN likes AS l1 ON p1.person_id = l1.person_id AND l1.food = 'Beer'
WHERE l1.person_id IS NULL

This attempts to join the each person with a record in likes with food = 'Beer', and then filters on non-matching records only.

Another option is to use a not exists condition with a correlated subquery:

SELECT p1.first_name, p1.last_name
FROM persons AS p1 
WHERE NOT EXISTS (
    SELECT 1
    FROM likes l1
    WHERE p1.person_id = l1.person_id AND l1.food = 'Beer'
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Newbie question: Problem with results, sql, join, where, "<" operator

SQL write a query, question from interview problem

Counting the number of times a result from a query shows up (basic SQL question)

How to write non correlated query with inner join in sql for this question?

Issue Retrieving results from SQL query in PHP

SQL query to get data of MCQ question and the answer from two table

SQL question, using an aggregate function inside a made table from a query

SQL - Create a join / query based on the results of another query. The Join table would be from the results of another query

SQL JOIN issue to discard results

sql left join with multiple same table question

{Basic Question} Applying Image grid with Bootstrap ( Thumbnail Issue)

SQL Select Query Question with Multiple Nested Condition

Hibernate shows question marks in SQL query

BigQuery and Google Analytics SQL query - expanded question

SQL question about combining different counts in a query

sql query execution order question for group by and select

Question about this SQL Server query of mine

Question About Query in SQL With 2 tables

Issue with sql query to get the results

How to write a SQL query that subtracts INNER JOIN results from LEFT JOIN results?

Issue in SQL query with Left Join

SQL join query result issue

Left Join Query issue of displaying duplicate results

SQL: How can I get more than one row from a different table with join? (Beginner's question)

AWS Lex - select query from user question

java basic question

Basic programming C Question

Basic bash printing question

Basic logical operator question