SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
-- create CREATE TABLE USERS ( user_id int, name varchar(15), email varchar(50) ); -- insert INSERT INTO USERS(user_id,name,email) VALUES (1, 'Ivan', 'join@example.com'); INSERT INTO USERS(user_id,name,email) VALUES (2, 'Mary', 'mary@example.com'); INSERT INTO USERS(user_id,name,email) VALUES (3, 'Step', 'step@example.com'); INSERT INTO USERS(user_id,name,email) VALUES (4, 'Ann', 'ann@example.com'); -- create CREATE TABLE ORDERS ( order_id int, user_id int, order_data int ); -- insert INSERT INTO ORDERS(order_id,user_id,order_data) VALUES (1, 1, 12); INSERT INTO ORDERS(order_id,user_id,order_data) VALUES (2, 3, 13); INSERT INTO ORDERS(order_id,user_id,order_data) VALUES (3, 2, 14); INSERT INTO ORDERS(order_id,user_id,order_data) VALUES (4, 1, 15); -- create CREATE TABLE ORDER_ITEMS ( item_id int, order_id int, product_name varchar(15), quantity int, price int ); -- insert INSERT INTO ORDER_ITEMS(item_id,order_id,product_name,quantity,price) VALUES (1, 1, 'Artishok', 2, 1000); INSERT INTO ORDER_ITEMS(item_id,order_id,product_name,quantity,price) VALUES (2, 1, 'Kruassan', 1, 200); INSERT INTO ORDER_ITEMS(item_id,order_id,product_name,quantity,price) VALUES (3, 2, 'Artishok', 3, 10); INSERT INTO ORDER_ITEMS(item_id,order_id,product_name,quantity,price) VALUES (4, 3, 'Mongostin', 1, 300); INSERT INTO ORDER_ITEMS(item_id,order_id,product_name,quantity,price) VALUES (5, 4, 'Kruassan', 2, 200); -- fetch SELECT USERS.name FROM USERS JOIN ORDERS ON USERS.user_id = ORDERS.user_id GROUP BY USERS.user_id ORDER BY COUNT(ORDERS.*) DESC LIMIT 1;

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear