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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:05:37+00:00 2026-06-16T00:05:37+00:00

How can I select count(*) from two different tables to create view? There are

  • 0

How can I select count(*) from two different tables to create view?

There are the tables:

DEPT table

  • DEPTNO
  • DNAME
  • LOC

EMP table

  • EMPNO
  • ENAME
  • JOB
  • MGR
  • HIREDATE
  • SAL
  • COMM
  • DEPTNO

I’ve tried:

CREATE VIEW PLACE
AS SELECT d.Loc CITY, count(d.Deptno) N_DEPT, count(e.Empno) N_EMP
   FROM Dept d, Emp e
   where d.Deptno = e.Deptno
   GROUP BY d.Loc, d.deptno;

and got this:

CITY          N_DEPT   N_EMP
CHICAGO         6       6
DALLAS          5       5
NEW YORK        3       3

There is only 1 N_DEPT in each city so the result is wrong :/.
There should be:

CITY          N_DEPT   N_EMP
CHICAGO         1       6
DALLAS          1       5
NEW YORK        1       3
  • 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-16T00:05:38+00:00Added an answer on June 16, 2026 at 12:05 am

    use DISTINCT, try

    CREATE VIEW PLACE
    AS 
    SELECT  d.Loc CITY, 
            count(DISTINCT d.Deptno) N_DEPT, 
            count(e.Empno) N_EMP
    FROM    Dept d 
            INNER JOIN Emp e
               ON d.Deptno = e.Deptno
    GROUP BY d.Loc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I select count(*) from two different tables (call them tab1 and tab2
I need to create a summary from 3 different tables, 1 parent table, 2
How can I do such query in Postgres? IF (select count(*) from orders) >
How can I select a COUNT based on ANOTHER ROW in the SAME TABLE
I have a complex query that joins different tables to get the count. There
I want to count two things under different conditions in one query. SELECT COUNT(*)
I have a page that pulls together aggregate data from two different tables. I
I need to select the counts from two tables that share a common column
Having tried this query a million different ways, I can't seem to SELECT 'count_lonely_vehicles'
Can someone explain me why this works: select val1, val2, count(case when table2.someID in

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.