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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:06:21+00:00 2026-06-13T20:06:21+00:00

Here is my tables Student (sname, sid, gpa, level, deptno) Course (cno, cname, deptno,

  • 0

Here is my tables

 Student (sname, sid, gpa, level, deptno) 
 Course (cno, cname, deptno, units) 
 Dept (dname, deptno) 
 Takes (sid, cno) 

Write a SQL query that returns the names (i.e., snames) of students who have taken
more courses outside their department than inside their department.you can
assume that all students in the database have taken at least one course inside their department.

I am not looking for any solutions for this question, but still welcome for any answer.
But I more hoping people can tell me how to generate the steps to write a complicated query like this..

My answer is

 Select S.sname
 From Student S, Course C, Dept D, Takes T
 Where T.cno=C.cno and D.deptno=C.deptno and S.sid = T.sid
 Having COUNT(S.deptno=C.deptno) > COUNT( S.deptno != C.deptno)

I am not sure I can use the count after HAVING in this way or not .
Thanks

  • 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-06-13T20:06:22+00:00Added an answer on June 13, 2026 at 8:06 pm

    Your first attempt, corrected:

     SELECT S.sname
     FROM Student S, Course C, Dept D, Takes T
     WHERE T.cno = C.cno AND D.deptno = C.deptno AND S.sid = T.sid
     GROUP BY S.sid
     HAVING COUNT(CASE WHEN S.deptno =  C.deptno THEN 1 ELSE NULL END) 
          < COUNT(CASE WHEN S.deptno <> C.deptno THEN 1 ELSE NULL END) ;
    

    and converted to SQL-92 syntax:

     SELECT S.sname
     FROM Student S 
       JOIN Takes T   ON S.sid = T.sid
       JOIN Course C  ON T.cno = C.cno
       JOIN Dept D    ON D.deptno = C.deptno
     GROUP BY S.sid        
            , S.sname                  --- this is not needed in SQL-2003
                                       --- but still required by most DBMS     
     HAVING 
            COUNT(CASE WHEN S.deptno =  C.deptno THEN 1 END) 
          < COUNT(CASE WHEN S.deptno <> C.deptno THEN 1 END) ;
    
                                       --- the ELSE NULL is not needed either
                                       --- it's the implied default
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a Student table I coded in postgreSQL (an excerpt): CREATE TABLE Student
I have a question. I have here 2 tables table 1 : products product_id
Here are the tables: CREATE TABLE [dbo].[Classes]( [ClassId] [int] NOT NULL, [ClassName] [nvarchar](50) NOT
Here are my tables in mysql: ----------------1------------------------- +---------------------------+----------+ | email | attempts | +---------------------------+----------+
Please look at this image here is 3 tables , and out i want
I need to make a synonym search function, using MySQL. Here is the tables
here's a Fusion Tables map that's broken after a search. It does everything well
here is my 3 tables: table 1 -- stores user information and it has
Here is my scenario (Tables): Orders ====================== Id (int) description (varchar) Products ====================== Id
Here is my scenario (tables): Departments ==================== Id (int) Name (varchar) Employees ==================== Id

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.