Examples of SQL queries can be used to study and practice writing SQL queries in MS Access.

Query Q031. An example of grouping records into two fields. The following SQL query calculates the amount and quantity of goods received from each vendor for each vendor:

SELECT supplier_id, product_id, SUM(amount) AS amount_sum, 
SUM(amount*price) AS income_sum
FROM m_income AS a INNER JOIN m_product AS b ON a.product_id=b.id
GROUP BY supplier_id, product_id;

Query Q032. Example of grouping records into two fields. The following query calculates for each vendor the amount and quantity of its products sold by us:

SELECT supplier_id, product_id, SUM(amount) AS amount_sum, 
SUM(amount*price) AS outcome_sum
FROM m_outcome AS a INNER JOIN m_product AS b ON a.product_id=b.id
GROUP BY supplier_id, product_id;

Query Q033. In this example, the two above queries (q031 and q032) are used as subqueries. The results of these queries by the LEFT JOIN method are combined into one report. The following query displays a report on the number and amount of incoming and sold products for each vendor. You should pay attention to the fact that if some product has already arrived, but has not yet been implemented, then the cell outcome_sum for this record will be empty. It should also be noted that this query serves only as an example of using relatively complex queries as a subquery. The performance of this SQL query for large amounts of data is questionable:

SELECT *
FROM
(SELECT supplier_id, product_id, SUM(amount) AS amount_sum,
SUM(amount*price) AS income_sum
FROM m_income AS a INNER JOIN m_product AS b
ON a.product_id=b.id GROUP BY supplier_id, product_id) AS a
LEFT JOIN
(SELECT supplier_id, product_id, SUM(amount) AS amount_sum,
SUM(amount*price) AS outcome_sum
FROM m_outcome AS a INNER JOIN m_product AS b
ON a.product_id=b.id GROUP BY supplier_id, product_id) AS b
ON (a.product_id=b.product_id) AND (a.supplier_id=b.supplier_id);

Query Q034. In this example, the two above queries (q031 and q032) are used as subqueries. The results of these queries by the RIGTH JOIN method are combined into one report. The following query displays a report on the amount of payments each client made on the payment systems used by him and the amount of his investment. The following query displays a report on the number and amount of incoming and sold products for each vendor. You should pay attention to the fact that if some product is already implemented, but has not yet arrived, the income_sum cell for this record will be empty. The presence of such empty cells is an indicator of a mistake in the accounting of sales, since before the sale, it is first necessary that the corresponding goods arrive:

SELECT *
FROM
(SELECT supplier_id, product_id, SUM(amount) AS amount_sum,
SUM(amount*price) AS income_sum
FROM m_income AS a INNER JOIN m_product AS b ON a.product_id=b.id
GROUP BY supplier_id, product_id) AS a
RIGHT JOIN
(SELECT supplier_id, product_id, SUM(amount) AS amount_sum,
SUM(amount*price) AS outcome_sum
FROM m_outcome AS a INNER JOIN m_product AS b ON a.product_id=b.id
GROUP BY supplier_id, product_id) AS b
ON (a.supplier_id=b.supplier_id) AND (a.product_id=b.product_id);

Query Q035. A report on the amount of revenue and expenses for products is displayed. To do this, create a list of products on the m_income and m_outcome tables, then for each product from this list, calculate the amount of its arrivals in the m_income table and the amount of its expenditures in the m_outcome table:

SELECT product_id, SUM(in_amount) AS income_amount, 
SUM(out_amount) AS outcome_amount
FROM
(SELECT product_id, amount AS in_amount, 0 AS out_amount
FROM m_income
UNION ALL
SELECT product_id, 0 AS in_amount, amount AS out_amount
FROM m_outcome) AS t
GROUP BY product_id;

Query Q036. The EXISTS function returns TRUE if the set it contains contains elements. The EXISTS function returns FALSE if the set passed to it is empty, that is, contains no elements. The following query displays the product codes that are contained in both the m_income table and the m_outcome table:

SELECT DISTINCT product_id
FROM m_income AS a
WHERE EXISTS(SELECT product_id FROM m_outcome AS b
WHERE b.product_id=a.product_id);

Query Q037. The product codes are displayed that are contained in both the m_income table and the m_outcome table:

SELECT DISTINCT product_id
FROM m_income AS a
WHERE product_id IN (SELECT product_id FROM m_outcome)

Query Q038. The product codes that are contained in the m_income table are displayed, but not contained in the m_outcome table:

SELECT DISTINCT product_id
FROM m_income AS a
WHERE NOT EXISTS(SELECT product_id FROM m_outcome AS b
WHERE b.product_id=a.product_id);

Query Q039. The list of products with the highest sales amount is displayed. The algorithm is as follows. For each product, the amount of its sales is calculated. Then, the maximum of these sums is determined. Then, for each product, the amount of its sales is calculated again, and the code and the amount of sales of goods whose sales amount is equal to the maximum are displayed:

SELECT product_id, SUM(amount*price) AS amount_sum
FROM m_outcome
GROUP BY product_id
HAVING SUM(amount*price) = (SELECT MAX(s_amount)
FROM (SELECT SUM(amount*price) AS s_amount FROM m_outcome GROUP BY product_id));

Query Q040. The reserved word IIF (conditional operator) is used to evaluate a logical expression and perform an action depending on the result (TRUE or FALSE). In the following example, the delivery of the goods is considered to be "small" if the quantity is less than 500. Otherwise, that is, the amount of receipt is greater than or equal to 500, the delivery is considered "large":

SELECT dt, product_id, amount, 
IIF(amount<500,"малая","большая") AS mark
FROM m_income;