From the course: SQL Server Fundamentals: Master Basic Query Techniques
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Solution: Case statement
From the course: SQL Server Fundamentals: Master Basic Query Techniques
Solution: Case statement
(upbeat music) - [Instructor] In this solution, I only need to retrieve data that's in the orders table, so I can go ahead and start filtering right away. So we'll say orders as O, and we know we want where the status equals paid, and now we can start writing our subquery, where o.Salesperson.Id is in, we'll write our subquery. Select salesperson ID from salesperson as S. Just for clarity, we'll add that alias to the column there, too, where s.FirstName equals Jane and s.LastName equals Porter. There, our filtering is done, so we'll work on our column list and our case statement, so we need the order ID, the total due. Now, for our case statement, we're going to say case when total due is less than 100, then we'll return one. When total due is between 100 and 150, then we'll return two, and otherwise, so just we'll use else, return three, end our case statement, and give a column alias as order tier, and we're all set. Give it a test, and we're all set!
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.