SQL Query HELP | Advice needed ! | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 3

SQL Query HELP | Advice needed !

Hello everyone! I am having trouble writing a query for the following Problem : App usage data are kept in the following table: TABLE sessions id INTEGER PRIMARY KEY, userId INTEGER NOT NULL, duration DECIMAL NOT NULL Write a query that selects userId and average session duration for each user who has more than one session. I am not sure I understand how to write the query that selects a userID that appears more than one times in the table. Any help would be great!

24th Aug 2017, 4:32 PM
Konstantinos Chatzidakis
Konstantinos Chatzidakis - avatar
5 Réponses
+ 6
Well, for sure I found a solution that works: SELECT userId, AVG(duration)from sessions GROUP BY userId HAVING COUNT(userId)>1 ; I am quoting from w3schools : The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. Thank you so much Michelle and Ankit for reaching out !
24th Aug 2017, 6:05 PM
Konstantinos Chatzidakis
Konstantinos Chatzidakis - avatar
+ 7
SELECT DISTINCT userId, AVG(duration) from sessions WHERE COUNT(userId)>1;
24th Aug 2017, 5:35 PM
Ankit Saxena
Ankit Saxena - avatar
+ 3
i'm not sure but try this if it works: SELECT DISTINCT userID, AVG(duration) FROM sessions WHERE (SELECT COUNT(userId) AS countUsers FROM sessions WHERE countUsers >1) let me know if it works... i can't test it now
24th Aug 2017, 5:23 PM
Michele Virgilio
Michele Virgilio - avatar
0
SELECT userId, AVG(duration)from sessions GROUP BY userId HAVING COUNT(userId)>1 ;
28th Nov 2017, 7:23 PM
Omkar Satpute
Omkar Satpute - avatar
0
Answer is Select * From users Limit 10.
11th Mar 2020, 12:32 AM
Imanuel Wicaksono
Imanuel Wicaksono - avatar