SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
#write a SQL Query to select all customers who did their first order within the last 24 hours create table customer ( Cust_id int, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, first_name varchar(64), last_name varchar(64) ); INSERT INTO customer VALUES ( 123, NOW(), now(), 'priya', 'Pandey' ), ( 134, NOW(), now(), 'pri12', 'Pandey' ), ( 193, NOW(), now(), 'pri56a', 'Pandey' ); create table orders ( Ord_id int, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, status varchar(64), cust_id int, prod_id int ); INSERT INTO orders VALUES (12453, now(), now(), 'deliver', 123, 2345), (13344, now(), now(), 'approve', 345, 3456), (13493, now(), now(), 'pending', 456, 4672); create table product ( Prod_id int, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, category varchar(64), name varchar(64), percent float ); INSERT INTO product VALUES (1243, now(), now(),'cloth','tshirt', 23), (1344, now(), now(), 'kids', 'frock', 23.44), (12493, now(), now(), 'kids', 'caps', 46.72); Select ord_id from orders where created_at between dateadd(hh,-24,current_timestamp) and current_timestamp and customer_id not in ( select distinct customer_id from orders where created_at < dateadd(hh,-24,current_timestamp))
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear