Author Archives: Febronei

100 Days of SQL

sql

Day 24 – SQL EXISTS Operator

The SQL EXISTS operator is a logical operator that is used to test the existence of a subquery. It is typically used in conjunction with a correlated subquery to determine if any rows exist in a table based on a condition specified in the subquery.

The syntax for the EXISTS operator is as follows:


SELECT column_name(s)
FROM table_name
WHERE EXISTS (SELECT column_name FROM table_name WHERE condition);

In this syntax, the main query retrieves data from a table, and the subquery checks for the existence of data in another table based on a specific condition. If the subquery returns any rows, the EXISTS operator returns true and the main query retrieves data as per the specified column names.

The EXISTS operator can be used with other SQL clauses, such as SELECT, UPDATE, DELETE, and INSERT. It is particularly useful when you want to filter results based on a condition that is not directly present in the table being queried. For example, you might use the EXISTS operator to find all customers who have placed orders in the last 30 days, even if their contact information is stored in a separate table.

100 Days of SQL

sql

Day 23 – SQL HAVING Clause

The SQL HAVING clause is used in conjunction with the GROUP BY clause to filter the results of a query based on the result of an aggregate function.

The syntax for the HAVING clause is as follows:


SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING aggregate_function(condition)

In this syntax, column_name(s) refers to the column(s) that you want to retrieve data from, table_name refers to the table that you want to retrieve data from, condition is an optional parameter that can be used to filter the results, and aggregate_function is a function that operates on the grouped data and returns a single result.

When you use the HAVING clause, the result set is first grouped based on the columns specified in the GROUP BY clause. Then, the aggregate function specified in the HAVING clause is applied to the grouped data. If the result of the aggregate function satisfies the condition specified in the HAVING clause, then the group is included in the result set.

For example, suppose you have a table called orders with columns customer_name, order_date, and order_amount. You could use the following query to group the orders by customer and retrieve the total amount of orders for each customer that is greater than 1000:


SELECT customer_name, SUM(order_amount)
FROM orders
GROUP BY customer_name
HAVING SUM(order_amount) > 1000

This query would return a result set that shows the total order amount for each customer in the orders table, but only for those customers whose total order amount is greater than 1000.

Note that the HAVING clause is used to filter the results of a query after the data has been grouped, whereas the WHERE clause is used to filter the results of a query before the data has been grouped.

100 Days of SQL

sql

Day 22 – SQL GROUP BY Statement

The SQL GROUP BY statement is used to group rows that have the same values in one or more columns, and perform aggregate functions on them.

The syntax for the GROUP BY statement is as follows:


SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)

In this syntax, column_name(s) refers to the column(s) that you want to group the results by, table_name refers to the table that you want to retrieve data from, and condition is an optional parameter that can be used to filter the results.

When you use the GROUP BY statement, the result set is divided into groups based on the values in the specified column(s). The aggregate functions (such as COUNT, SUM, AVG, MAX, and MIN) are then applied to each group, and the result is returned as a single row for each group.

For example, suppose you have a table called orders with columns customer_name, order_date, and order_amount. You could use the following query to group the orders by customer and calculate the total amount of orders for each customer:


SELECT customer_name, SUM(order_amount)
FROM orders
GROUP BY customer_name

This query would return a result set that shows the total order amount for each customer in the orders table.

Note that when you use the GROUP BY statement, all columns in the SELECT statement must either be included in the GROUP BY clause or be included in an aggregate function.

100 Days of SQL

sql

Day 21 – SQL FULL OUTER JOIN Keyword

The SQL FULL OUTER JOIN keyword is used to combine rows from two or more tables, including all the rows from both tables, regardless of whether there is a matching row in the other table(s).

The syntax for a FULL OUTER JOIN is as follows:


SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;

In this syntax, table1 and table2 are the names of the tables that you want to join, and column_name is the name of the column that you want to join the tables on.

The result of a FULL OUTER JOIN is a table that contains all the rows from both tables, with NULL values in the columns where there is no matching row in the other table(s). If there is a matching row, the columns from both tables are combined into a single row in the result set.

Note that not all database management systems support the FULL OUTER JOIN keyword. In such cases, you can simulate a FULL OUTER JOIN using a combination of a LEFT OUTER JOIN and a RIGHT OUTER JOIN.

100 Days of SQL

sql

Day 20 – SQL RIGHT JOIN Keyword

In SQL, the RIGHT JOIN keyword is used to combine all the rows from the right table with matching rows from the left table based on a related column between them. If there is no match, the result will contain NULL values for the left table columns.

Here’s an example of how to use the RIGHT JOIN keyword:


SELECT *
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;

In this example, table1 is the left table, table2 is the right table, and column is the related column between the two tables. The ON keyword specifies the condition for the join.

The result of the RIGHT JOIN operation is a new table that includes all the rows from the right table and the matching rows from the left table. If there is no match, the result will contain NULL values for the left table columns.

Here’s an example of how to use column aliases with a RIGHT JOIN:


SELECT table1.column1 AS column1_alias, table2.column2 AS column2_alias
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;

In this example, column1_alias and column2_alias are the aliases given to the duplicated columns in the result table. This makes it easier to refer to the columns in the SELECT statement and the result table.

Note that in some databases, such as MySQL, the RIGHT OUTER JOIN keyword can be used interchangeably with RIGHT JOIN. The same is true for LEFT JOIN and LEFT OUTER JOIN.

100 Days of SQL

sql

Day 19 – SQL LEFT JOIN Keyword

In SQL, the LEFT JOIN keyword is used to combine all the rows from the left table with matching rows from the right table based on a related column between them. If there is no match, the result will contain NULL values for the right table columns.

Here’s an example of how to use the LEFT JOIN keyword:


SELECT *
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;

In this example, table1 is the left table, table2 is the right table, and column is the related column between the two tables. The ON keyword specifies the condition for the join.

The result of the LEFT JOIN operation is a new table that includes all the rows from the left table and the matching rows from the right table. If there is no match, the result will contain NULL values for the right table columns.

Here’s an example of how to use column aliases with a LEFT JOIN:


SELECT table1.column1 AS column1_alias, table2.column2 AS column2_alias
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;

In this example, column1_alias and column2_alias are the aliases given to the duplicated columns in the result table. This makes it easier to refer to the columns in the SELECT statement and the result table.

Note that in some databases, such as MySQL, the LEFT OUTER JOIN keyword can be used interchangeably with LEFT JOIN. The same is true for RIGHT JOIN and RIGHT OUTER JOIN.

100 Days of SQL

sql

Day 18 – SQL INNER JOIN Keyword

In SQL, the INNER JOIN keyword is used to combine rows from two or more tables based on a related column between them. The INNER JOIN returns only the rows that have matching values in both tables.

Here’s an example of how to use the INNER JOIN keyword:


SELECT *
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

In this example, table1 and table2 are the names of the two tables being joined, and column is the related column between the two tables. The ON keyword specifies the condition for the join.

The result of the INNER JOIN operation is a new table that includes all the columns from both tables where the join condition is true. The columns that are not used for the join will be duplicated in the result table. You can use column aliases to differentiate between the duplicated columns.

Here’s an example of how to use column aliases with an INNER JOIN:


SELECT table1.column1 AS column1_alias, table2.column2 AS column2_alias
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

In this example, column1_alias and column2_alias are the aliases given to the duplicated columns in the result table. This makes it easier to refer to the columns in the SELECT statement and the result table.

100 Days of SQL

sql

Day 17 – SQL Joins

In SQL, a JOIN operation is used to combine rows from two or more tables based on a related column between them. The JOIN operation is a fundamental concept in SQL and is essential for querying data from multiple tables in a relational database.

There are several types of JOIN operations available in SQL, including:

  • Inner Join: Returns only the rows that have matching values in both tables.
  • Left Join: Returns all the rows from the left table and the matched rows from the right table. If there is no match, the result will contain NULL values for the right table columns.
  • Right Join: Returns all the rows from the right table and the matched rows from the left table. If there is no match, the result will contain NULL values for the left table columns.
  • Full Outer Join: Returns all the rows from both tables, matching them where possible and adding NULL values where there is no match.

The syntax for a JOIN operation depends on the type of join and the database system you’re using. Here’s an example of an inner join:


SELECT *
FROM table1
JOIN table2
ON table1.column = table2.column;

In this example, table1 and table2 are the names of the two tables being joined, and column is the related column between the two tables. The ON keyword specifies the condition for the join.

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

Note that the result of a join operation is a new table that includes all the columns from both tables. The columns that are not used for the join will be duplicated in the result table. You can use column aliases to differentiate between the duplicated columns.

Join operations can be used in combination with other SQL operations like WHERE, GROUP BY, and ORDER BY to create complex queries that retrieve specific data from multiple tables.

100 Days of SQL

sql

Day 16 – SQL Aliases

In SQL, an alias is a temporary name given to a table or column in a query. Aliases are commonly used to make column names more readable or to simplify complex queries.

To create an alias for a table or column in a query, you can use the AS keyword followed by the desired alias name. Here’s an example:


SELECT column1 AS alias1, column2 AS alias2 FROM table_name;

In this example, the SELECT statement returns two columns from a table called table_name, but the columns are given temporary aliases alias1 and alias2.

You can also use aliases to simplify complex queries that involve multiple tables. For example, if you want to join two tables called orders and customers, you can use aliases to refer to each table without having to type out the full table name every time. Here’s an example:


SELECT o.order_id, c.customer_name
FROM orders AS o
INNER JOIN customers AS c ON o.customer_id = c.customer_id;

In this example, the orders and customers tables are given temporary aliases o and c, respectively. This makes it easier to refer to each table in the SELECT statement and the JOIN clause.

Note that aliases are temporary and only apply to the current query. If you want to use the same alias in a different query, you’ll need to define it again.

100 Days of SQL

sql

Day 15 – SQL BETWEEN Operator

In SQL, the BETWEEN operator is used to specify a range of values in a WHERE clause. The BETWEEN operator allows you to specify a range of values for a column, and the WHERE clause will return any rows where the column value falls within that range.

The basic syntax for using the BETWEEN operator is as follows:


SELECT column_name FROM table_name WHERE column_name BETWEEN value1 AND value2;

Here, column_name is the name of the column you want to search, table_name is the name of the table containing the column, and value1 and value2 are the two values that define the range.

For example, suppose you have a table called employees with columns employee_id, first_name, last_name, department, and salary. If you want to find all employees with salaries between $50,000 and $100,000, you would use the following SQL statement:


SELECT * FROM employees WHERE salary BETWEEN 50000 AND 100000;

This statement will return all rows from the employees table where the salary column falls within the range of $50,000 to $100,000.

Note that the BETWEEN operator is inclusive, meaning that it includes the values specified in the range. So in the example above, the SQL statement will return all employees with salaries of exactly $50,000 or $100,000, in addition to those with salaries between those values.

You can also use the NOT BETWEEN operator to exclude rows that fall within a certain range, or you can use the BETWEEN operator with a NOT keyword to exclude rows that do not fall within a certain range.