Skip to content
Advertisement

How to join two tables and add a limit

Given two tables in a SQLite database,

app_vendor

app_id | vendor_id 
---------------
43     |  747
3      |  12
83     |  747

....

app

id | name
---------------
43 | Instagram
5  | Snapchat

and I am trying to get 10 ‘names’ from ‘app’ (names of apps) for a given ‘vendor_id’

SELECT A.name 
FROM app_vendor AS S 
WHERE vendor_id = 747 
JOIN app AS A ON S.app_id = A.id 
LIMIT 10

What am I doing wrong?

Advertisement

Answer

Your WHERE clause is in the wrong location. You can do:

SELECT A.name 
FROM app_vendor AS S 
JOIN app AS A ON S.app_id = A.id 
WHERE s.vendor_id = 747 
LIMIT 10

Result:

name      
--------- 
Instagram 
Snapchat  

See running example at DB Fiddle.

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