13 Common SQL Queries For Managing Data - Udemy Blog
13 Common SQL Queries For Managing Data - Udemy Blog
If you don’t know how to swim, the deep end of the pool at summer camp can seem like a very
a stunning and refreshing place on a sunny day, if you know how to swim.
Similarly, a huge database can be overwhelming if you don’t have an efficient way to sort, manage, and find the data you need. That same d
possibilities if you know the most common and simple queries of SQL.
SQL makes it easy to work with massive databases without getting overwhelmed or needing to spend hours manually scrolling through Ex
SQL queries that you can use to find, sort, arrange, modify, and manage data in a database of any size.
Before we start talking about them, we should realize that these queries work on all types of SQL engines available in the market.
For a simple metaphor, think about how we turn a car while driving. When you want to turn your vehicle, you rotate the car’s steering whee
taking place within the wheel’s axle. Similarly, when you use SQL, you only have to fire simple queries to retrieve and manage data from yo
with behind-the-scenes internal database operations.
Explore Course
SQL provides you with an easy-to-understand interface, allowing you to communicate with your database efficiently.
You should wisely choose a SQL engine that will be compatible with your company’s database systems. If not sure what option will be best,
i i
What is a SQL query?
When writing in the SQL language, you will use a variety of SQL keywords to create statements and queries.
A statement involves a string of characters and SQL keywords that conforms to the formatting and syntax rules of the language and may af
You can think of a statement as a “complete sentence” in the SQL language that can be successfully sent to a database management system
A query reflects a special type of statement written to retrieve data based on specific criteria. The result of writing a SQL query will be a set
You will often rely on SQL keywords to help you define the criteria in your query.
CREATE TABLE student (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);
This query will create a table called “student” with zero rows and three columns. This table will store records of students. It will have student
like this:
Here, the column ID, set as a primary key, means that the ID column can have only unique values, and cells in this column cannot be left bl
When writing this type of query, you must define the character type after the title of each column. In the example above, the Name column,
characters. The Age column, defined in the query as “INTEGER,” means it can only hold numbers (not letters or special characters).
You will first type INSERT INTO, followed by the name of the table you are working with. Then, in parentheses, list the columns that will cont
Then, you will type VALUES and, in parentheses, define the exact values that will be filled in the respective columns, in the same order that y
written inside single quotation marks, separated by commas.
Let’s say you want to add a new student named Alan, aged 28, to your student table.
INSERT INTO student (id, name, age) VALUES (‘1’, ‘Alan’, 28);
After firing the above query, our table will look like this:
ID Name Age
1 Alan 28
We can fire more such queries to fill records in our table. For example:
insert into student (id, name, age) values (‘2’, ‘Amy’, ‘26’);
insert into student (id, name, age) values (‘3’, ‘Bob’, ‘27’);
3 Bob 27
4 Chris 28
5 Dan 26
Use the SQL keyword FROM to define the name of the table from which you’d like to see records.
The result of this query will be a display of all rows present in the table you named:
ID Name Age
1 Alan 28
2 Amy 26
3 Bob 27
4 Chris 28
5 Dan 26
ID Name Age
2 Amy 26
5 Dan 26
3 Bob 27
1 Alan 28
4 Chris 28
As you can see above, the output is arranged in increasing order by age. The ORDER BY clause will automatically arrange the records in incr
in decreasing order, you can simply insert the SQL keyword DESC keyword into your query immediately after the column name.
ID Name Age
1 Alan 28
4 Chris 28
3 Bob 27
2 Amy 26
5 Dan 26
ID Name Age
1 Alan 28
4 Chris 28
You can also use the SELECT query with the SUM keyword to receive the sum of a numeric column. For example, you can fire:
You will receive 135 as output (the sum of all of the ages in your table).
Keep in mind: you can only use MAX, MIN, and SUM functions with numeric columns. If you try to use these functions with a text column, yo
This query will look at the table called “Student” and will delete the entire row (or multiple rows) wherever the Name column has the value
In our case, the result of this query will be the following table:
SQL stud
MySQL SQL Server
Data Science
ID Name Age
2 Amy 26
3 Bob 27
4 Chris 28
5 Dan 26
Note that if the column referenced contains characters (as the Name column does), you must specify the specific value within single quotat
ID Name Age
2 Amy 22
3 Bob 27
4 Chris 28
5 Dan 26
8. Viewing records from a table without knowing exact details (LIKE)
In real life, when you interact with a database, you probably may not know all of the exact column values. For example, as a data operator in
your school from hearing other teachers talking about her. Perhaps you want to find the entire records for Kellie, but you do not know how
In this case, you can use the LIKE operator in SQL to help you find a row of data when you only know some of the characters included.
The output of this query will show you all rows of students where the data in the Name column begins with “Kell” and ends with any letters
information, not sure of the exact details.
ID Name Age
2 Amy 22
3 Bob 27
4 Chris 28
5 Dan 26
6 Dan 24
Now, if we would like to see the new student Dan’s records, we may fire our query as:
ID Name Age
5 Dan 26
6 Dan 24
As you can see above, you were unable to fetch a unique record just by using the Name value in the WHERE clause. In this case, there exist m
column.
Now, consider combining more than one condition in the WHERE clause. This can be easily done using conditional keywords like AND and
ID Name Age
6 Dan 24
By defining both the Name and Age values and by connecting them with the SQL keyword, AND, you are able to isolate one person named
You can also combine the AND and OR conditions in the WHERE clause to refine your search even further. Let’s say that you want to find an
student is over 25 years old. You could fire the following query:
ID Name Age
3 Bob 27
4 Chris 28
5 Dan 26
6 Dan 24
As you can see above, the result will include all students with the name Dan as well as all students with an age above 25.
You can use different conditions like AND, OR and < , > in a combination or individually in WHERE clause to fetch the desired rows.