r94yan
r94yan

Reputation: 15

where clause is not narrowing down the records to what i have specified

Have some tables, and am trying to add up the number of records up, but from a particular office.

SQL :

  SELECT  COUNT(*) AS "Number of employees" FROM staff, office
  WHERE office.office_id  =10;

so want the employees from office = 10. So the field should display as "Number of employees" as 3.

Instead it adds all records up and displays "9".

Office 1 has an ID of 10 office 2 has an ID of 20

Upvotes: 0

Views: 47

Answers (2)

Abhi
Abhi

Reputation: 98

You must be having some relation between the two tables. Something like officeId in staff table?

select count(*) from staff,office where office.office_id=staff.office_id and office.office_id=10;

The first condition is to link the tables and the second one to filter it.

Upvotes: 1

Erol
Erol

Reputation: 91

You have to connect 2 tables to get the result you want such as:

   SELECT  COUNT(*) AS "Number of employees" FROM staff, office
   WHERE staff.office_id = office.office_id and office.office_id  =10;

Upvotes: 1

Related Questions