Data Query Language (DQL)

Data Query Language (or simply DQL) consists of commands that perform data selection, which is the main focus of relational database users in the world of SQL. The statement used is SELECT that can be accompanied by other clauses or options so that your extracted results will be in an organized and readable format. You can submit a query to the database using a separate application interface or just a single command-line.

SELECT [* | ALL | DISTINCT COLUMN1, COLUMN2]SELECT * FROM customer;

--select everything from customer
SELECT * FROM customer;

-- select id and FirstName column from the customer table
SELECT id, FirstName FROM customer;

WHERE statement

When you want to be more specific in selecting rows of data from your database tables then you need to add a bit of complexity to your programming lines. At this point, you need the function of the WHERE clause, which means that the SELECT operation will be performed once the stated condition inside such clause is true.

--select everything where the quantity is greater or equal to 20 or the year in order date is 2015
SELECT * FROM orderDetails
WHERE quantity >= 20 OR
YEAR(order_date) = 2015;

--select everything from orderDetails where the values in quantity is between 10 and 20
SELECT * FROM orderDetails
WHERE quantity BETWEEN 10 AND 20;

ORDER BY and GROUP BY Statements

When you want the data you retrieve to be displayed and sorted in some way, then you need to include the ORDER BY or GROUP BY operator at the end of your SQL statement. The primary function of the ORDER BY statement is basically to arrange data using a specific order, whether ascending or descending. On the other hand, the GROUP BY statement is used to put identical data together and arrange the query output into groups.

SELECT COLUMN_LIST
FROM TABLE_NAME ORDER BY COLUMN_LIST [ASC | DESC]

SELECT * FROM orderDetails
ORDER BY quantity DESC;

SELECT product , count(*) FROM orderDetails
GROUP BY product;

In the example above, the COUNT function was introduced to arrange the data in groups. The following is a summary of the common aggregate functions used together with the GROUP BY statement (x denotes the column name where you want to perform the function):

  • AVG(x) – computes the average of all the column values (null values

    removed)

  • COUNT(x) – counts the number of non-null values in the column

  • COUNT(*) – counts the number of records

  • MAX(x) – computes the maximum value in the column (null values

    removed)

  • MIN(x) - computes the minimum value in the column (null values

    removed)

  • SUM(x) – computes the sum or total of the values in the column (null

    values ignored)

  • IN - returns the records that match the values in the IN statement

    SELECT * FROM orderDetails
    WHERE product IN ('product 1', 'product 2');
    
    --filters product 1 and 2 and returns other products in the product column
    SELECT * FROM orderDetails
    WHERE product NOT IN ('product 1', 'product 2');