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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:01:28+00:00 2026-05-20T03:01:28+00:00

Consider the following tables: department deptid (type:INT) deptname (type: TEXT) hours (type:INT) active (type:BIT)

  • 0

Consider the following tables:

department

deptid      (type:INT)
deptname    (type: TEXT)
hours       (type:INT)
active      (type:BIT)

employee

empid       (type:INT)
empname     (type: TEXT)
deptid      (type: INT)
designation (type: TEXT)
salary      (type: INT)

Write a query to return the columns empname and deptname of the employees belonging to those
departments that have a head count of 4 or more. The records should be returned in alphabetical order of empname

This was my take:

SELECT e1.empname, d.deptname from employee AS e1
FULL JOIN department AS d on e1.deptid = d.deptid
  WHERE e1.deptid IN(
    SELECT deptid FROM(
      SELECT e2.deptid, COUNT(e2.empid)
      FROM employee AS e2
      GROUP BY e2.deptid
      HAVING COUNT(e2.empid) >= 4
    )
  )
ORDER BY empname;

How would you improve on 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-20T03:01:28+00:00Added an answer on May 20, 2026 at 3:01 am

    This is shorter and probably performs faster too

    SELECT e1.empname, d.deptname
    from (
          SELECT e2.deptid
          FROM employee AS e2
          GROUP BY e2.deptid
          HAVING COUNT(e2.empid) >= 4
        ) G
    inner join employee AS e1 on e1.deptid = G.deptid
    INNER JOIN department AS d on d.deptid = G.deptid
    ORDER BY e1.empname;
    

    Start with the grouping. You don’t need COUNT from the inner query.
    Then, join to both tables just to get the names.

    INNER JOIN is used because once the count is complete, we already know that

    1. the employees exist
    2. the department exists
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following tables in MySQL database: entries: creator_id INT entry TEXT is_expired BOOL other:
Consider the following scenario: Tables: Employee (EmpId(PK), Name) TeamMembers(TeamId(PK), EmpId(PK)) Project(ProjId(PK), TeamId) I really
Consider the following table: mysql> select * from phone_numbers; +-------------+------+-----------+ | number | type
Consider the following table: create table temp ( name int, a int, b int
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following tables for a LMS: Item ( ID BIGINT NOT NULL UNIQUE
Consider the following tables (screenshot of dbml file editor in Visual Studio): http://roosteronacid.com/dbml.png The
Consider the following 2 tables: Table A: id event_time Table B id start_time end_time
Consider the following database tables: Table messages with 13,000,000 rows (one row per message).
Consider following piece of code: declare @var bit = 0 select * from tableA

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.