1
- Write an SQL query to find username and count of invoices from user and invoice table.
1
+ -- Write an SQL query to find username and count of invoices from user and invoice table.
2
2
3
- Return the result table in any order.
4
- # Write your MySQL query statement below
3
+ -- Return the result table in any order.
4
+ -- Write your MySQL query statement below
5
+ -- create a table
6
+ CREATE TABLE user (
7
+ id INTEGER PRIMARY KEY ,
8
+ name TEXT NOT NULL ,
9
+ gender TEXT NOT NULL
10
+ );
11
+
12
+ CREATE TABLE invoice (
13
+ id INTEGER PRIMARY KEY ,
14
+ name TEXT NOT NULL ,
15
+ total INTEGER NOT NULL ,
16
+ user_id INTEGER ,
17
+ FOREIGN KEY (user_id) REFERENCES user(id)
18
+ );
19
+ -- insert some values
20
+ INSERT INTO user VALUES (1 , ' Ryan' , ' M' );
21
+ INSERT INTO user VALUES (2 , ' Joanna' , ' F' );
22
+ INSERT INTO user VALUES (3 , ' Saul' , ' M' );
23
+ INSERT INTO user VALUES (4 , ' Norma' , ' F' );
24
+
25
+ INSERT INTO invoice VALUES (1 , ' backery' , 99 ,1 );
26
+ INSERT INTO invoice VALUES (2 , ' meat' , 88 ,1 );
27
+ INSERT INTO invoice VALUES (3 , ' candy' , 77 ,3 );
28
+ INSERT INTO invoice VALUES (4 , ' soaps' , 66 ,4 );
29
+ INSERT INTO invoice VALUES (5 , ' meat' , 55 ,2 );
30
+ INSERT INTO invoice VALUES (6 , ' candy' , 44 ,2 );
31
+ INSERT INTO invoice VALUES (7 , ' soaps' , 33 ,2 );
32
+ -- fetch some values
33
+ SELECT user .name , COUNT (* ) as count
34
+ FROM user
35
+ INNER JOIN invoice ON user .id = invoice .user_id
36
+ GROUP BY user .id
37
+ HAVING COUNT (* ) > 1 ;
0 commit comments