0

I have the following table for example:

Table users:

|Id |User  |
| 1 |Frank |
| 2 |Tom   |
| 3 |Lisa  |

Table cities:

|Id |City  |
| 1 |Paris |
| 2 |Tokyo |

How can I have the count of each table users and cities in only one SQL query ?

I want to have:
- Number of users: 3
- Number of cities: 2

Thanks.

2
  • What results are you trying to get? Also, please add a tag for the specific RDBMS you're using (mysql, sql-server, Oracle, etc.). Commented May 31, 2014 at 14:10
  • Are you sure you wanted to use the word sum? Did you really mean count? Commented May 31, 2014 at 14:14

1 Answer 1

2

Just guessing what you mean:

SELECT (SELECT COUNT(*) FROM users) AS user_count,
       (SELECT COUNT(*) FROM cities) AS city_count

In general, you can use a subquery any place where an expression is allowed, as long as the subquery returns 1 row with 1 column, by wrapping the subquery in parentheses.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.