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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:21:07+00:00 2026-05-20T11:21:07+00:00

I have 4 tables: users, userpreference, userinfo, useredu the last three tables use id

  • 0

I have 4 tables: users, userpreference, userinfo, useredu

the last three tables use “id” as a foreign key referencing the table ‘users’ :

Query to be formulated:

i need to find the “top music among all single females who go to MSU”

noting that MSU could also be ‘Minnesota State University”

i have this query so far but it is not producing the correct results?

select userpreference.preferencevalue as 'Music', COUNT(*) as 'SingleFemaleCount'from users, userpreference, userinformation
where users.Id = userinformation.Id
and users.Id = userpreference.Id
and userpreference.Id = userinformation.Id
and users.Gender = 'female'
and userinformation.informationvalue = 'single'
and usereducation.school like 'msu%' OR like 'minnesota state%'
and userpreference.preferencetype = 'music' GROUP BY preferencevalue ORDER BY      COUNT(distinct users.Id) DESC limit 10
  • 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-20T11:21:08+00:00Added an answer on May 20, 2026 at 11:21 am

    It might be as simple as you need some parenthesis in your where clause:

    (usereducation.school like 'msu%' OR like 'minnesota state%')

    Otherwise, the OR will be of lower precedence than the adjacent ANDs.

    EDIT: 2011-03-06

    Below, I have formatted the code to make it a little easier to read, and also moved the userinformation and usereducation checking into exists() clauses. The reason I am doing this is because if a user has more than 1 userinformation or usereductionat row matching your criteria, it would affect the count() aggregate.

    select
        userpreference.preferencevalue as 'Music',
        COUNT(*) as 'SingleFemaleCount'
    
    from users, userpreference
    where users.Gender = 'female'
      and userpreference.Id = users.Id
      and userpreference.preferencetype = 'music'
    
      and exists
        (select *
        from userinformation
        where userinformation.Id = users.Id
          and userinformation.informationvalue = 'single')
    
      and exists
        (select *
        from usereducation
        where usereducation.Id = users.Id
          and (usereducation.school like 'msu%' OR like 'minnesota state%'))
    
    GROUP BY userpreference.preferencevalue
    ORDER BY COUNT(*) DESC limit 10
    

    Another thing to check is that (usereducation.school like 'msu%' OR like 'minnesota state%') indeed finds all MSU records. If the result set is not too huge, would run a select distinct school from usereducation to check and see that sure you are getting all records.

    Lastly, I sort of prefer to use the join syntax as follows:

    select
        userpreference.preferencevalue as 'Music',
        COUNT(*) as 'SingleFemaleCount'
    
    from users
    inner join userpreference on userpreference.Id = users.Id
    where users.Gender = 'female'
      and userpreference.preferencetype = 'music'
    
      and exists
        (select *
        from userinformation
        where userinformation.Id = users.Id
          and userinformation.informationvalue = 'single')
    
      and exists
        (select *
        from usereducation
        where usereducation.Id = users.Id
          and (usereducation.school like 'msu%' OR like 'minnesota state%'))
    
    GROUP BY userpreference.preferencevalue
    ORDER BY COUNT(*) DESC limit 10
    

    I realize I totally changed your query, but hey this is homework, right 🙂

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

Sidebar

Related Questions

I have three tables...users, user_info, and quota_levels. They look like this: CREATE TABLE users
I have three tables: Users, Groups and GroupsUsers User table: id: 1 username: admin
I have 3 tables: users, images (foreign key to user_id), comments (foreign keys: user_id
I have three tables: 'users', 'products', and 'product_types'. The 'users' table stores all usual
I have three database tables: users emails invitations Emails are linked to users by
I have 2 tables (Users, Wall). The UserID in the Wall table is a
I have two tables users registered_members I want to confirm values from user table
I have two tables Users Users_Role I decided to try to add a foreign
I have 2 tables: Users and Roles, and I have a table that joins
I have three tables (users, articles, and tags) defined in SQLAlchemy and mapped with

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.