sql - subqueries
In previous chapter i have explained How To Use JOIN Statement In SQL
now i am gonna eplain you How To Use Subqueries Statement In SQL
To accomplish this, we are first going to introduce a built-in SQL function, MAX(). This function wraps around a table column and quickly returns the current highest (max) value for the specified column. We are going to use this function to return the current "highest", aka most recent date value in the orders table.
SQL Subquery Preview:
USE mydatabase;
SELECT MAX(day_of_order)
FROM orders
SQL Results:
day_of_order |
2008-08-16 00:00:00.000 |
Now we can throw this query into the WHERE clause of another SELECTquery and obtain the results to our little dilemma.
SQL Select Subquery Code:
USE mydatabase;
SELECT *
FROM orders
WHERE day_of_order = (SELECT MAX(day_of_order) FROM orders)
:
id | customer | day_of_order | product | quantity |
3 | A+Maintenance | 2008-08-16 00:00:00.000 | Hanging Files | 14 |
This query is a dynamic query as it pulls current information and will change if a new order is placed. Utilizing a subquery we were able to build a dynamic and robust solution for providing us with current order information.
No comments:
Post a Comment