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 1084131
In Process

The Archive Base Latest Questions

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

I have a database with 2 tables: Table 1: CREATE TABLE IF NOT EXISTS

  • 0

I have a database with 2 tables:

Table 1:

CREATE TABLE IF NOT EXISTS `sales` (
  `sale_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `sale_total` int(11) NOT NULL,
  `sale_date` date NOT NULL,
  `sale_status` int(11) NOT NULL,
  PRIMARY KEY (`sale_id`)
) ;

Table 2:

CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL,
  `lastname` varchar(200) NOT NULL,
  `mail` varchar(200) NOT NULL,
  PRIMARY KEY (`user_id`)
);

I need the following query optimized, so that it does not use subqueries. I this this could be accomplished using joins, but I don’t know exactly how.

SELECT name, lastname, mail
FROM users
WHERE user_id IN (
   SELECT user_id
   FROM sales
   WHERE sale_date < '2009-01-01'
   AND sale_total >100
   AND sale_status =4
)
AND user_id NOT IN (
    SELECT user_id
    FROM sales
    WHERE sale_date >= '2009-01-01'
)
  • 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-16T22:31:25+00:00Added an answer on May 16, 2026 at 10:31 pm

    Use joins to replace IN subqueries, null-left-joins to replace NOT IN, and GROUP BY to return only one row for each user:

    SELECT users.name, users.lastname, users.mail
    FROM users
    JOIN sales AS s0 ON s0.user_id=users.user_id
    LEFT JOIN sales AS s1 ON s1.user_id=users.user_id AND sale_date>='2009-01-01'
    WHERE s1.sale_id IS NULL
    AND s0.sale_date < '2009-01-01' AND s0.sale_total>100 AND s0.sale_status=4
    GROUP BY users.user_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have in my MySQL database these two tables: CREATE TABLE IF NOT EXISTS
I have implemented a linked list as a self-referencing database table: CREATE TABLE LinkedList(
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
I have an Access database in which I drop the table and then create
I have database with many tables. In the first table, I have a field
In my database I have tables that define types for example Table: Publication Types
I have two tables in my database: NEWS table with columns: id - the
I have many tables in my database which are interrelated. I have a table
I have two simple tables in my database. A card table that contains Id,
I have to create a database with two tables in MySQL, but the script

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.