Fundamentals

Fundamentals

1. AS

The AS keyword is used to assign an alias to a column or a table.

Syntax: Column Alias

SELECT column_name AS alias_name
FROM table_name;

Syntax: Table Alias

SELECT column_name
FROM table_name AS alias_name;
2. DISTINCT

DISTINCT is used remove duplicate rows from the result set of a query, returning only unique values.

It can be applied to one or more columns in a query to ensure that the combination of these columns’ values is unique in the returned results.

Syntax:

SELECT DISTINCT column1, column2, ...
FROM table_name;
3. WHERE

WHERE is used to filter records that fulfil a specified condition.

Syntax:

SELECT * FROM Customers
WHERE [conditions]
4. BETWEEN

BETWEEN selects value in a given range where the values stated are inclusive.

Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example: Return patient’s first and last name that has weight between 100 and 120.

SELECT first_name, last_name
FROM patients
WHERE weight BETWEEN 100 AND 120;
5. ORDER BY

Sort the result in ascending (default) or descending (DESC) order.

Syntax:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC]
6. RAND

RAND is used to generate a random floating-point number between 0 (inclusive) and 1 (exclusive).

Example: Generating a Random Number

SELECT RAND(100) AS random_value;
  • To generate a single random number between 0 and 1:
  • (100) is a seed that makes the random number generation predictable and reproducible

Example: Select Random Rows

SELECT *
FROM customers
ORDER BY RAND();
  • The ORDER BY RAND() clause sorts the rows in random order.
7. LIMIT

Used to specify the maximum number of rows that a query should return.

Syntax:

SELECT column_name(s)
FROM table_name
LIMIT number_of_rows

Example:

SELECT * 
FROM employees 
LIMIT 10;
  • This query returns the first 10 rows from the employees table.
8. OFFSET

It is used in SQL to skip a certain number of rows before returning the remaining results. It’s often used in conjunction with the LIMIT.

Syntax:

SELECT column_name(s)
FROM table_name
LIMIT number_of_rows [OFFSET start_position];

Example:

SELECT *
FROM your_table
LIMIT 10 OFFSET 5;
  • The LIMIT clause specifies the maximum number of rows to return, which is 10 rows (or fewer)
  • The OFFSET clause specifies the number of rows to skip from the top, which is 5.
9. LIKE / ILIKE

Perform pattern matching against string data with wildcard characters. LIKE is case sensitive, ILIKE is case insensitive.

Syntax:

SELECT column1, column2, ...
FROM table_name
WHERE columnn (I)LIKE pattern:
10. AND
  • Purpose: The AND operator is used to combine multiple conditions in such a way that all conditions must be true for the row to be included in the result set.
  • Behavior: If all conditions connected by AND are true, the overall expression evaluates to true; otherwise, it evaluates to false.

Example

SELECT * 
FROM employees 
WHERE department = 'Sales' 
AND salary > 50000;
  • This query selects rows where both conditions are true: the department is ‘Sales’ and the salary is greater than 50,000.
11. OR
  • Purpose: The OR operator is used to combine multiple conditions in such a way that if any one of the conditions is true, the row will be included in the result set.
  • Behavior: If at least one condition connected by OR is true, the overall expression evaluates to true; if all conditions are false, it evaluates to false.

Example

SELECT * 
FROM employees 
WHERE department = 'Sales' 
OR department = 'HR';
  • This query selects rows where either condition is true: the department is ‘Sales’ or the department is ‘HR’.
12. NOT
  • Purpose: The NOT operator is used to negate a condition, meaning it inverts the result of the condition.
  • Behavior: If the condition is true, NOT makes it false; if the condition is false, NOT makes it true.

Example

SELECT * 
FROM employees 
WHERE NOT department = 'HR';
  • This query selects rows where the department is not ‘HR’.
13. IN

The IN operator is used to simplify complex queries that need to check whether a value in a column matches any value in a given list.

Example:

SELECT customer_id, first_name, city
FROM customers
WHERE city IN ('New York', 'Los Angeles', 'Chicago');
  • This query returns all rows where the city is either “New York,” “Los Angeles,” or “Chicago.”
14. NULL

Filter records that contains null values.

SELECT * FROM _tabble
WHERE col IS (NOT) NULL
15. If the LIMIT 10 and OFFSET 5, how many rows will the query return?
While the LIMIT clause specifies returning 10 records, the OFFSET clause in the query skips the first 5 records. Therefore, the results will display records from the 6th row of the data to the 15th row (10 rows)
16. MIN, MAX, COUNT, SUM, AVG

Aggregate Functions definitions:

  • 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
17. GROUP BY

GROUP BY is a SQL clause used to group rows based on one or more columns. It’s often used in conjunction with aggregate functions like SUM, AVG, COUNT, MIN, and MAX to summarise data.

Example:

SELECT department, SUM(amount) AS total_sales
FROM sales
GROUP BY department;
  • The query returns the total sales amount per department.
18. HAVING

HAVING allow us to filter the groups based on aggregate values after an aggregation has already taken place.

Example:

SELECT department, SUM(amount) AS total_sales
FROM sales
GROUP BY department
HAVING SUM(amount) > 1000;
  • The query returns departments where the total sales amount exceeds $1000.
19. Explain the difference between WHERE and HAVING.
  • HAVING filters those groups based on conditions applied to the aggregated data.
  • WHERE filters individual rows before they are grouped.
20. INNER JOIN
The INNER JOIN returns records that have matching values in both tables.
21. LEFT JOIN
The LEFT JOIN returns all records from the left table, and the matched records from the right table. If there is no match with right table, the results are null.
22. RIGHT JOIN
The RIGHT JOIN returns all records from the right table, and the matched records from the left table. If there is no match with left table, the results are null.
23. FULL JOIN
The FULL JOIN returns all records when there is a match in either left or right table.
Last updated on 22 Aug 2024