Category Archives: Programming Language

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.

100 Days of SQL

sql

Day 14 – SQL IN Operator

In SQL, the IN operator is used to specify multiple values in a WHERE clause. The IN operator allows you to specify a list of values, and the WHERE clause will return any rows where the specified column matches any of the values in the list.

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


SELECT column_name FROM table_name WHERE column_name IN (value1, value2, value3, ...);

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, value2, value3, etc. are the values you want to search for.

For example, suppose you have a table called employees with columns employee_id, first_name, last_name, and department. If you want to find all employees who work in the “Sales” or “Marketing” departments, you would use the following SQL statement:


SELECT * FROM employees WHERE department IN ('Sales', 'Marketing');

This statement will return all rows from the employees table where the department column matches either “Sales” or “Marketing”.

The IN operator can also be used with a subquery to search for values in another table. For example, suppose you have a table called sales with columns product_id, product_name, and price, and you want to find all products with a price higher than the average price. You could use the following SQL statement:


SELECT product_name, price FROM sales WHERE price > (SELECT AVG(price) FROM sales);

This statement will return all rows from the sales table where the price column is higher than the average price of all products in the sales table.

100 Days of SQL

sql

Day 13 – SQL LIKE Operator

In SQL, the LIKE operator is used to search for patterns in a column of text data. The LIKE operator is often used in combination with wildcard characters to search for specific patterns.

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


SELECT column_name FROM table_name WHERE column_name LIKE pattern;

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 pattern is the search pattern you want to use.

There are two wildcard characters that can be used with the LIKE operator:

  1. % (percent sign): This represents zero or more characters. For example, the pattern '%test%' would match any string that contains the word “test” anywhere in the column.
  2. _ (underscore): This represents a single character. For example, the pattern 'a_%' would match any string that starts with the letter “a” followed by any single character.

For example, suppose you have a table called employees with columns employee_id, first_name, last_name, and email_address. If you want to find all employees whose email address ends with “@example.com”, you would use the following SQL statement:


SELECT * FROM employees WHERE email_address LIKE '%@example.com';

This statement will return all rows from the employees table where the email_address column ends with “@example.com”.