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

In the SQL query, the SELECT statement is used to select from database tables.

SQL Query Q001. Example of an SQL query to retrieve only the required fields in the desired sequence:

SELECT dt, product_id, amount
FROM m_income;

SQL Query Q002. In this SQL query example, the asterisk (*) is used to display all columns of the m_product table, in other words, for obtaining all fields of the relation m_product:

SELECT *
FROM m_product;

SQL Query Q003. The DISTINCT statement is used to exclude duplicate records and retrieve many unique records:

SELECT DISTINCT product_id
FROM m_income;

SQL Query Q004. The ORDER BY clause is used to sort (order) records based on the values of a particular field. The field name is indicated by the ORDER BY clause:

SELECT *
FROM m_income
ORDER BY price;

SQL Query Q005. The ASC instruction is used as an addition to the ORDER BY clause and serves to determine the sorting in ascending order. The DESC instruction is used as a complement to the ORDER BY clause and serves to define sorting in descending order. In the case where neither ASC nor DESC is specified, ASC (default) is implied:

SELECT *
FROM m_income
ORDER BY dt DESC , price;

SQL Query Q006. To select the necessary records from the table, use different logical expressions that express the selection condition. The logical expression is given after the WHERE clause. Example of obtaining from the m_income table of all records for which the amount is greater than 200:

SELECT *
FROM m_income
WHERE amount>200;

SQL Query Q007. To express complex conditions use logical operations AND (conjunction), OR (disjunction) and NOT (logical negation). Example of obtaining from the table m_outcome all records for which the value amount is 20 and the price value is greater than or equal to 10:

SELECT dt, product_id, amount, price
FROM m_outcome
WHERE amount=20 AND price>=10;

SQL Query Q008. To combine the data of two or more tables, use the INNER JOIN, LEFT JOIN, RIGHT JOIN instructions. The following example retrieves the dt, product_id, amount, price fields from the m_income table and the title field from the m_product table. The record of the m_income table is connected to the record of the m_product table when the value m_income.product_id is equal to the value m_product.id:

SELECT dt, product_id, title, amount, price
FROM m_income INNER JOIN m_product
ON m_income.product_id=m_product.id;

SQL query Q009. In this SQL query, you need to pay attention to two things: 1) the search text is enclosed in single quotes ('); 2) the date is in the format # Month / Day / Year #, which is true for MS Access. In other systems, the format of the date spelling may be different. Example of output of information on milk intake on June 12, 2011. Note the date format #6/12/2011#:

SELECT dt, product_id, title, amount, price
FROM m_income INNER JOIN m_product
ON m_income.product_id=m_product.id
WHERE title='Молоко' And dt=#6/12/2011#;

SQL Query Q010. The BETWEEN statement is used to verify the membership of a range of values. An example of an SQL query that displays information about goods received between the 1st and the 30th of June 2011:

SELECT *
FROM m_income INNER JOIN m_product
ON m_income.product_id=m_product.id
WHERE dt BETWEEN #6/1/2011# And #6/30/2011#;