31 Examples To Master SQL. Unlocking The Power of SQL PanData Level Up Coding
31 Examples To Master SQL. Unlocking The Power of SQL PanData Level Up Coding
com/31-examples-to-master-sql-839514c093d0
Search Medium
� panData Follow
Save
SQL (Structured Query Language) is a powerful programming language that is used to manage and
manipulate data in relational databases. It has become an essential tool for data professionals, as it allows
them to perform tasks such as retrieving, inserting, updating, and deleting data in a efficient and organized
manner.
In today’s highly data-driven business world, SQL is a highly sought after skill, and individuals who have a
strong understanding of SQL are in high demand in a variety of industries, including finance, healthcare,
1 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
and technology.
Whether you’re working as a data analyst, database administrator, or software developer, having a solid
understanding of SQL can help you advance your career and stay ahead of the curve in a rapidly evolving job
market.
It covers a wide range of topics, including data modeling, queries, joins, and advanced techniques like
window functions and SQL loops, also known as iterative statements, are used to repeatedly execute a block
of SQL code until a specific condition is met.
Relational Data
A relational database consists of multiple tables that relate to each other. The relationship between tables is
formed in the sense of shared columns.
Database Management
There are many different relational database management systems (MySQL, PostgreSQL, SQL Server). The
SQL syntax between them may differ slightly.
Practical Approach
• Create a database and tables
• Update tables
We will be asked to enter the password. Now we are connected to the MySQL server on our machine.
2. Create a Database
2 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
We are not yet in the retail database that does not yet contain tables.
3. Create tables
First, we will create a table called “client” using the create table command.
We define the name of the columns and associated data types within the parenthesis. The column cust_id is
specified as the primary key.
The primary key is the column that uniquely identifies each row. It’s like the index of a Pandas DataFrame.
In the beginning, we mentioned that relational tables are related to each other through shared columns. A
column that lists two tables is a foreign key.
The column cust_id in the orders table is a foreign key and relates the order table to the customer table. We
specify this condition when creating the table.
3 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
5. View Tables
The retail database contains two tables now. We can see the existing tables in a database using the show
tables command.
show tables;
+------------------+
| Tables_in_retail |
+------------------+
| customer |
| orders |
+-----------------+
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| order_id | int(11) | NO | PRI | NULL | |
| date | date | YES | | NULL | |
| amount | decimal(5,2) | YES | | NULL | |
| cust_id | int(11) | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
7. Modify Tables
We can modify existing tables. For example, the change table command can add a new column or delete an
existing one. For example, let’s add a column to the order table called “is_sale.”
We type dread the column name and data type, along with the additional keyword.
desc orders;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
4 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
+----------+--------------+------+-----+---------+-------+
| order_id | int(11) | NO | PRI | NULL | |
| date | date | YES | | NULL | |
| amount | decimal(5,2) | YES | | NULL | |
| cust_id | int(11) | YES | MUL | NULL | |
| is_sale | varchar(20) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
8. Delete Feature
We can also use the changing table to delete a column with a slight change in syntax.
The drop keyword is used instead of the add. It is because we also don’t need to write the data type to drop a
column.
9. Enter data
We have tables, but they don’t contain data. One way to populate tables is the insertion statement.
The specified values are entered in the columns in the same order. So, we need to maintain consistent order.
5 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
of 17.
If we do not specify a condition, all rows in the given table are deleted.
+----------+------------+--------+---------+
| order_id | date | amount | cust_id |
+----------+------------+--------+---------+
| 1 | 2020-10-01 | 24.40 | 1001 |
+----------+------------+--------+---------+
It is the first row in the order table. We want to change the order value to 27.40.
update orders
set amount = 27.40 #alterar essa coluna
where order_id = 1;
select * from orders limit 1;
+----------+------------+--------+---------+
| order_id | date | amount | cust_id |
+----------+------------+--------+---------+
| 1 | 2020-10-01 | 27.40 | 1001 |
We write the updated values after the keyword set. The rows to be updated are identified by providing the
conditions after the keyword where.
6 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
+------------------+
The table orders_copy has the same structure as the order table but contains no data.
It Looks like a combination of two separate statements. The first row creates the table, and the second row
fills it with the data in the order table.
We have two relational tables in a database. The following examples will demonstrate how we can retrieve
data from these tables using selected queries.
7 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
The “*” selects all columns, and the limit keyword places a constraint on the number of rows to display.
20. Sort
We may want to sort query results that can be done using order by clause. For example, the following query
8 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
For example, we can count the number of unique days in the order table.
9 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
The order table contains orders on four different days — the keyword “as” is used to rename the column in
the query result. Otherwise, the column name would be “count(data).”
23. Group By
There are four different days in the order table. We can also find out how many orders per day. Group by will
help us in this task.
10 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
It is important to note that the order of the statements in the query matters. For example, it gives an error if
we place the order by clause before they have a clause.
We want to see the difference between the maximum order and the minimum order for each customer. We
also want to sort the results based on the difference in the ascending order and display the first three.
+---------+-------+
| cust_id | dif |
+---------+-------+
| 1007 | 46.00 |
| 1009 | 28.95 |
11 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
| 1002 | 24.35 |
+---------+-------+
The dif column is obtained by subtracting the minimum value from the maximum value.
+----------+--------+----------------+
| location | gender | count(cust_id) |
+----------+--------+----------------+
| Austin | female | 2 |
| Austin | male | 1 |
| Dallas | female | 2 |
| Dallas | male | 2 |
| Houston | female | 2 |
| Houston | male | 1 |
+----------+--------+----------------+
29. Relationship
Customer and order tables are related to each other based on the cust_id. Therefore, we can query data from
both tables using JOINS.
We want the average order value for each city in the customer table.
Because we select columns from two different tables, column names are specified with the associated table
name. The second, third, and fourth rows of the above query join the order and customer table based on the
cust_id in each table.
12 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
+---------+
| avg_age |
+---------+
| 30.0000 |
+---------+
We can use codenames for the names on the table as well. It is useful when we need to type table names
often.
Concluding
I believe the 30 examples in this article provide a comprehensive introduction to SQL. In addition, we cover
the following topics:
• Modifying tables
13 of 14 4/13/2023, 1:30 PM
� 31 Examples to Master SQL. Unlocking the Power of SQL: 31… |... https://levelup.gitconnected.com/31-examples-to-master-sql-839514c093d0
It is best to move on to the more advanced operations since you are comfortable working with the basics.
Leonardo Anello
in/anello92
133 1
By signing up, you will create a Medium account if you don’t already have one. Review
our Privacy Policy for more information about our privacy practices.
14 of 14 4/13/2023, 1:30 PM