Hello Community,
I wasnt quite sure how to phrase the title of this question, because my main point of this question is not simply to find the question to the query that I'm about post, but how to approach the answer to query.
The query is a follows:
Find the customer with the highest total order cost between 2019-02-01 to 2019-05-01. Output their first name, total cost of their items, and the date.
The tables are as follows:
CREATE TABLE orders (
id int,
cust_id int,
order_date date,
order_quantity int,
order_details nvarchar(50),
order_cost int)
INSERT orders VALUES
(1,3,CONVERT(DATETIME, '2019-03-04', 120),4,N'Coat',100),
(2,3,CONVERT(DATETIME, '2019-03-01', 120),1,N'Shoes',80),
(3,3,CONVERT(DATETIME, '2019-03-04', 120),1,N'Skirt',30),
(4,7,CONVERT(DATETIME, '2019-02-01', 120),1,N'Coat',100),
(5,7,CONVERT(DATETIME, '2019-03-10', 120),1,N'Shoes',80),
(6,15,CONVERT(DATETIME, '2019-02-01', 120),2,N'Boats',100),
(7,15,CONVERT(DATETIME, '2019-01-11', 120),3,N'Shirts',60),
(8,15,CONVERT(DATETIME, '2019-03-11', 120),1,N'Slipper',20),
(9,15,CONVERT(DATETIME, '2019-03-01', 120),2,N'Jeans',80),
(10,15,CONVERT(DATETIME, '2019-03-09', 120),3,N'Shirts',50),
(11,5,CONVERT(DATETIME, '2019-02-01', 120),1,N'Shoes',80),
(12,12,CONVERT(DATETIME, '2019-01-11', 120),3,N'Shirts',60),
(13,12,CONVERT(DATETIME, '2019-03-11', 120),1,N'Slipper',20),
(14,4,CONVERT(DATETIME, '2019-02-01', 120),1,N'Shoes',80),
(15,4,CONVERT(DATETIME, '2019-01-11', 120),3,N'Shirts',60),
(16,3,CONVERT(DATETIME, '2019-04-19', 120),3,N'Shirts',50),
(17,7,CONVERT(DATETIME, '2019-04-19', 120),1,N'Suit',150),
(18,15,CONVERT(DATETIME, '2019-04-19', 120),1,N'Skirt',30),
(19,15,CONVERT(DATETIME, '2019-04-19', 120),2,N'Dresses',200),
(20,12,CONVERT(DATETIME, '2019-01-11', 120),5,N'Coat',20),
(21,7,CONVERT(DATETIME, '2019-04-01', 120),1,N'Suit',50),
(22,7,CONVERT(DATETIME, '2019-04-02', 120),1,N'Skirt',30),
(23,7,CONVERT(DATETIME, '2019-04-03', 120),1,N'Dresses',50),
(24,7,CONVERT(DATETIME, '2019-04-04', 120),1,N'Coat',25)
SELECT * FROM orders
CREATE TABLE customers (
id int,
first_name nvarchar(50),
last_name nvarchar(50),
city nvarchar(50),
address nvarchar(50),
phone_number nvarchar(50))
INSERT customers VALUES
(8,N'John',N'Joseph',N'Arizona',N'',N'928-386-8164'),
(7,N'Jill',N'Michael',N'Florida',N'',N'813-297-0692'),
(4,N'William',N'Daniel',N'Colorado',N'',N'813-368-1200'),
(5,N'Henry',N'Jackson',N'Hawaii',N'',N'808-601-7513'),
(13,N'Emma',N'Isaac',N'Hawaii',N'',N'808-690-5201'),
(14,N'Liam',N'Samuel',N'Hawaii',N'',N'808-555-5201'),
(15,N'Mia',N'Owen',N'Hawaii',N'',N'808-640-5201'),
(1,N'Mark',N'Thomas',N'Arizona',N'4476 Parkway Drive',N'602-993-5916'),
(12,N'Eva',N'Lucas',N'Arizona',N'4379 Skips Lane',N'301-509-8805'),
(6,N'Jack',N'Aiden',N'Arizona',N'4833 Coplin Avenue',N'480-303-1527'),
(2,N'Mona',N'Adrian',N'California',N'1958 Peck Court',N'714-409-9432'),
(10,N'Lili',N'Oliver',N'California',N'3832 Euclid Avenue',N'530-695-1180'),
(3,N'Farida',N'Joseph',N'Florida',N'3153 Rhapsody Street',N'813-368-1200'),
(9,N'Justin',N'Alexander',N'Colorado',N'4470 McKinley Avenue',N'970-433-7589'),
(11,N'Frank',N'Jacob',N'Hawaii',N'1299 Randall Drive',N'808-590-5201')
SELECT * FROM customers
As you've probably guess by now, I have a rudimentary understanding of T-SQL, and while I can often find the answer to simple queries, this query requires deep thought, and I would like someone to demonstrate how they approach solving this query?
Let me know if you would like me to post the suggested answer to the query, but I would welcome your suggestions before I post the answer
Thanks in advance