avatarMarat Miftakhov

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2947

Abstract

</span>;</pre></div><p id="43f0">This updates the values in the columns <code>column1</code> and <code>column2</code> to <code>value1</code> and <code>value2</code>, respectively, for all rows that meet the specified condition.</p><h2 id="e1b4">Deleting Data</h2><p id="85b3">To delete data from a table, you can use the DELETE FROM statement.</p><div id="35ce"><pre><span class="hljs-keyword">DELETE</span> <span class="hljs-keyword">FROM</span> table_name <span class="hljs-keyword">WHERE</span> <span class="hljs-keyword">condition</span>;</pre></div><p id="c46c">This deletes all rows from the table <code>table_name</code> that meet the specified condition.</p><h2 id="f55c">JOIN</h2><p id="6e43">The JOIN command in SQL is used to combine rows from two or more tables based on a related column between them. There are several different types of JOINs in SQL, including INNER JOIN, OUTER JOIN, and CROSS JOIN.</p><p id="754e"><b>INNER JOIN</b>: An INNER JOIN returns only the rows that match the join condition in both tables. It is the default type of JOIN if no specific type is specified.</p><div id="f2bb"><pre><span class="hljs-keyword">SELECT</span> <span class="hljs-operator"></span> <span class="hljs-keyword">FROM</span> table1 <span class="hljs-keyword">INNER</span> <span class="hljs-keyword">JOIN</span> table2 <span class="hljs-keyword">ON</span> table1.common_column <span class="hljs-operator">=</span> table2.common_column;</pre></div><p id="79bb">This statement returns all rows from both tables that have a matching value in the <code>common_column</code> column.</p><p id="2f0d"><b>OUTER JOIN</b>: An OUTER JOIN returns all rows from both tables, whether or not there is a match in the join condition. There are two types of OUTER JOINs: <b>LEFT JOIN</b> and <b>RIGHT JOIN</b>. A LEFT JOIN returns all rows from the left table (<code>table1</code>) and any matching rows from the right table (<code>table2</code>). A RIGHT JOIN returns all rows from the right table and any matching rows from the left table. Here is an example of a LEFT JOIN:</p><div id="8221"><pre><span class="hljs-keyword">SELECT</span> <span class="hljs-operator"></span> <span class="hljs-keyword">FROM</span> table1 <span class="hljs-keyword">LEFT</span> <span class="hljs-keyword">JOIN</span> table2 <span class="hljs-keyword">ON</span> table1.common_column <span class="hljs-operator">=</span> table2.common_column;</pre></div><p id="6001">This statement returns all rows from <code>table1</code>, as well as any matching rows from <code>table2</code>. If there is no match in table2, NULL values will be returned for the right table’s columns.</p><p id="94bd"><b>CROSS JOIN</b>: A CROSS JOIN returns the Cartesian product of the two tables. In other words, it returns all possible combinations of rows from both tables. Here is an example:</p><div id="e9cb"><pre><span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <s

Options

pan class="hljs-keyword">FROM</span> table1 <span class="hljs-keyword">CROSS</span> <span class="hljs-keyword">JOIN</span> table2;</pre></div><p id="c2f7">This statement returns all possible combinations of rows from both <code>table1 </code>and <code>table2</code>.</p><h2 id="a0e1">Aggregate functions</h2><p id="8954">Aggregate functions in SQL are used to perform calculations on a set of values. Let’s take a look at SUM and AVG.</p><p id="3df9"><b>SUM</b>: The SUM function calculates the total of a set of values. It takes a column name as an argument and returns the sum of all the values in that column.</p><div id="6852"><pre><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">SUM</span>(column_name) <span class="hljs-keyword">FROM</span> table_name;</pre></div><p id="a381">This statement returns the sum of all the values in the <code>column_name</code> column of the <code>table_name</code> table.</p><p id="2ea3"><b>AVG</b>: The AVG function calculates the average of a set of values. It also takes a column name as an argument and returns the average of all the values in that column.</p><div id="c313"><pre><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">AVG</span>(column_name) <span class="hljs-keyword">FROM</span> table_name;</pre></div><p id="1098">This statement returns the average of all the values in the <code>column_name</code> column of the <code>table_name</code> table.</p><p id="1928">Both SUM and AVG can be used with a WHERE clause to specify a condition for which rows should be included in the calculation.</p><div id="8cee"><pre><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">SUM</span>(column_name) <span class="hljs-keyword">FROM</span> table_name <span class="hljs-keyword">WHERE</span> <span class="hljs-keyword">condition</span>;</pre></div><p id="1628">This statement returns the sum of all the values in the <code>column_name </code>column of the <code>table_name</code> table, but only for rows that meet the specified condition.</p><h2 id="c068">Conclusion</h2><p id="9f47">These are just a few basic examples of SQL statements, but there are many more features and capabilities of the language. SQL is an essential tool for working with data in relational databases, and is used by businesses, organizations, and individuals all over the world.</p><div id="e4c7" class="link-block"> <a href="https://www.buymeacoffee.com/marateek"> <div> <div> <h2>Marat Miftakhov</h2> <div><h3>If you found this useful you can buy me a coffee. I appreciate your support!</h3></div> <div><p>www.buymeacoffee.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*WDkPWbWr5DTn5xJz)"></div> </div> </div> </a> </div></article></body>

SQL 101: The Ultimate Beginner’s Guide to the World of Relational Databases

SQL (Structured Query Language) is a programming language used to manage and manipulate data stored in relational databases. It is the standard language for interacting with databases, and is used to create, modify, and query databases.

Basic Syntax

SQL statements are made up of a combination of keywords, clauses, and functions. Keywords are used to specify actions, such as SELECT, FROM, WHERE, and UPDATE. Clauses are used to specify the objects that the action will be applied to, such as table names and column names. Functions are used to perform calculations and manipulate data, such as SUM, AVG, and CONCAT.

Here is an example of a simple SQL statement:

SELECT column1, column2
FROM table_name
WHERE condition;

This statement selects the data in the columns column1 and column2 from the table table_name and returns only the rows that meet the specified condition.

Creating Tables

To create a new table in a database, you can use the CREATE TABLE statement.

CREATE TABLE table_name (
  column1 datatype,
  column2 datatype,
  column3 datatype
);

This creates a new table named table_name with three columns, column1, column2, and column3, each with a specified data type.

Inserting Data

To insert data into a table, you can use the INSERT INTO statement. Here is an example:

INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);

This inserts a new row into the table table_name, with the values value1, value2, and value3 in the columns column1, column2, and column3, respectively.

Updating Data

To update data in a table, you can use the UPDATE statement.

UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;

This updates the values in the columns column1 and column2 to value1 and value2, respectively, for all rows that meet the specified condition.

Deleting Data

To delete data from a table, you can use the DELETE FROM statement.

DELETE FROM table_name
WHERE condition;

This deletes all rows from the table table_name that meet the specified condition.

JOIN

The JOIN command in SQL is used to combine rows from two or more tables based on a related column between them. There are several different types of JOINs in SQL, including INNER JOIN, OUTER JOIN, and CROSS JOIN.

INNER JOIN: An INNER JOIN returns only the rows that match the join condition in both tables. It is the default type of JOIN if no specific type is specified.

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

This statement returns all rows from both tables that have a matching value in the common_column column.

OUTER JOIN: An OUTER JOIN returns all rows from both tables, whether or not there is a match in the join condition. There are two types of OUTER JOINs: LEFT JOIN and RIGHT JOIN. A LEFT JOIN returns all rows from the left table (table1) and any matching rows from the right table (table2). A RIGHT JOIN returns all rows from the right table and any matching rows from the left table. Here is an example of a LEFT JOIN:

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

This statement returns all rows from table1, as well as any matching rows from table2. If there is no match in table2, NULL values will be returned for the right table’s columns.

CROSS JOIN: A CROSS JOIN returns the Cartesian product of the two tables. In other words, it returns all possible combinations of rows from both tables. Here is an example:

SELECT *
FROM table1
CROSS JOIN table2;

This statement returns all possible combinations of rows from both table1 and table2.

Aggregate functions

Aggregate functions in SQL are used to perform calculations on a set of values. Let’s take a look at SUM and AVG.

SUM: The SUM function calculates the total of a set of values. It takes a column name as an argument and returns the sum of all the values in that column.

SELECT SUM(column_name)
FROM table_name;

This statement returns the sum of all the values in the column_name column of the table_name table.

AVG: The AVG function calculates the average of a set of values. It also takes a column name as an argument and returns the average of all the values in that column.

SELECT AVG(column_name)
FROM table_name;

This statement returns the average of all the values in the column_name column of the table_name table.

Both SUM and AVG can be used with a WHERE clause to specify a condition for which rows should be included in the calculation.

SELECT SUM(column_name)
FROM table_name
WHERE condition;

This statement returns the sum of all the values in the column_name column of the table_name table, but only for rows that meet the specified condition.

Conclusion

These are just a few basic examples of SQL statements, but there are many more features and capabilities of the language. SQL is an essential tool for working with data in relational databases, and is used by businesses, organizations, and individuals all over the world.

Sql
Relational Databases
Database
Database Development
Software Engineering
Recommended from ReadMedium