Skip to content
Advertisement

How do I add a count() for one table into a SQL query that has joins for multiple other tables using SQLite?

I have a sql statement that pulls data from multiple tables. This query gets all employees and their biographic information (employee id, first name, last name, badge image, etc.):

Now, there is another table called, say, EmployeeDiscrepencies that I would like to do a count on:

I would like to add this count statement into my original query when I grab all employees, but I cannot figure out how to do it. I wanted to try something like this:

As you can guess, it is not working

How do I go about accomplishing this?

Advertisement

Answer

If you want only that counter from EmployeeDiscrepencies then the simplest way is to use a subquery to return it like this:

If your condition is for employeeId = 2 only then a cross join of your query to the subquery would be more efficient:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement