SQL IN Operator

The SQL IN Operator

The IN operator allows you to specify multiple values in a
WHERE
clause.

The IN operator is a shorthand for multiple
OR
conditions.

Example

Return all customers from ‘Germany’, ‘France’, or ‘UK’

SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');

Continue reading SQL IN Operator

SQL Wildcards

SQL Wildcard Characters

A wildcard character is used to substitute one or more characters in a string.

Wildcard characters are used with the
LIKE operator. The LIKE operator is used in a
WHERE
clause to search for a specified pattern in a column.

Example

Return all customers that starts with the letter ‘a’:

SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';

Continue reading SQL Wildcards

SQL AVG() Function

The SQL AVG() Function

The AVG() function returns the average value of a numeric column.

Example

Find the average price of all products:

SELECT AVG(Price)
FROM Products;

Note: NULL values are ignored.


Continue reading SQL AVG() Function

SQL SUM() Function

The SQL SUM() Function

The SUM() function returns the total sum of a numeric column.

Example

Return the sum of all Quantity fields in the OrderDetails table:

SELECT SUM(Quantity)
FROM OrderDetails;

Continue reading SQL SUM() Function

SQL COUNT() Function

The SQL COUNT() Function

The COUNT() function returns the number of rows that matches a specified criterion.

Example

Find the total number of rows in the Products table:

SELECT COUNT(*)
FROM Products;

Continue reading SQL COUNT() Function

SQL MIN() and MAX() Functions

The SQL MIN() and MAX() Functions

The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

MIN Example

Find the lowest price in the Price column:

SELECT MIN(Price)
FROM Products;

Continue reading SQL MIN() and MAX() Functions

SQL Aggregate Functions

SQL Aggregate Functions

An aggregate function is a function that performs a calculation on a set of values, and returns a single value.

Aggregate functions are often used with the GROUP
BY
clause of the SELECT statement. The GROUP BY clause splits the result-set into groups of values and the aggregate function can be used to return a single value for each group.

The most commonly used SQL aggregate functions are :

  • MIN() – returns the smallest value within the selected column
  • MAX() – returns the largest value within the selected column
  • COUNT() – returns the number of rows in a set
  • SUM() – returns the total sum of a numerical column
  • AVG() – returns the average value of a numerical column

Continue reading SQL Aggregate Functions

SQL TOP, LIMIT, FETCH FIRST or ROWNUM Clause

The SQL SELECT TOP Clause

The SELECT TOP clause is used to specify the number of records to return.

The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.

Example

Select only the first 3 records of the Customers table:

SELECT TOP 3 * FROM Customers;

Note: Not all database systems support the
SELECT TOP
clause. MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses
FETCH FIRST
n ROWS ONLY and ROWNUM.

Continue reading SQL TOP, LIMIT, FETCH FIRST or ROWNUM Clause