Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1001261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:38:31+00:00 2026-05-16T07:38:31+00:00

I have a table with some ice cream order data like so: id order_number

  • 0

I have a table with some ice cream order data like so:

id  order_number  item_name
1   1             cake cone
2   1             sprinkles
3   2             sugar cone
4   3             semi-sweet morsels
5   3             sprinkles

(This is simpler than the real table but the relevant columns are present.)

Essentially what I want to do is generate a list of order numbers that don’t contain particular items (sugar cones and cake cones).

What I’ve tried at this point seems to work but I wonder if there is a better way:

select a.order_number from 
(select order_number from orders where (item_name != 'sugar cone' and item_name != 'cake cone') group by order_number)a
left join
(select order_number from orders where (item_name = 'sugar cone' or item_name = 'cake cone') group by order_number)b
on a.order_number = b.order_number
where b.order_number is null;

This will return me the order_numbers I expect (in this case, only order 3) but it seems clunky to me.

Do you have a better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:38:32+00:00Added an answer on May 16, 2026 at 7:38 am

    Using LEFT JOIN/IS NULL:

       SELECT x.order_number
         FROM ORDERS x
    LEFT JOIN ORDERS y ON y.order_number = x.order_number
                      AND y.item_name IN ('sprinkles', 'sugar cone')
        WHERE y.id IS NULL
    

    Using NOT EXISTS

       SELECT x.order_number
         FROM ORDERS x
        WHERE NOT EXISTS(SELECT NULL
                           FROM ORDERS y
                          WHERE y.item_name IN ('sprinkles', 'sugar cone')
                            AND y.order_number = x.order_number)
    

    Using NOT IN

       SELECT x.order_number
         FROM ORDERS x
        WHERE x.order_number NOT IN(SELECT y.order_number
                            FROM ORDERS y
                          WHERE y.item_name IN ('sprinkles', 'sugar cone'))
    

    Summary

    On MySQL, if the column(s) being joined on are not nullable – LEFT JOIN/IS NULL is the fastest/most efficient. Otherwise, NOT EXISTS & NOT IN are better choices.

    Conclusion

    Because of joining on a column that can not be NULL, LEFT JOIN/IS NULL is the best choice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with some data that looks like: id | type |
I have a table that saves some account limits like users. For most rows
I have a database table full of some really ugly and messy data. In
I have some data grouped in a table by a certain criteria, and for
I have to delete some rows from a data table. I've heard that it
I have a table with some data in it: ColA | ColB | ColC
I have a table that contains some blob fields that I don't want to
I have a table with say 1640 items. I set bindingSource.Filter = some filter
Let's say you have a table for branches in your organization. Some of them
I have some char() fields in a DBF table that were left encrypted by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.