Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

13 Common SQL Queries For Managing Data - Udemy Blog

SQL provides a simple interface to communicate efficiently with databases. Some common SQL queries include CREATE TABLE to make a new table, INSERT INTO to add records to a table, SELECT to view all records from a table, ORDER BY to arrange the records in a particular order, and SELECT COUNT to view only selected records from large tables. These basic SQL statements allow users to define, modify and extract data from relational databases.

Uploaded by

titobesaleel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

13 Common SQL Queries For Managing Data - Udemy Blog

SQL provides a simple interface to communicate efficiently with databases. Some common SQL queries include CREATE TABLE to make a new table, INSERT INTO to add records to a table, SELECT to view all records from a table, ORDER BY to arrange the records in a particular order, and SELECT COUNT to view only selected records from large tables. These basic SQL statements allow users to define, modify and extract data from relational databases.

Uploaded by

titobesaleel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Databases represent large swimming pools.

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. 

Demystifying SQL: What is it?


SQL, or Structured Query Language, serves as a programming language designed to help you communicate with relational databases in yo
and queries to manage, organize, and update data in a database.

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. 

The Complete SQL Boo


Last Updated May 2022

Bestseller  83 lectures  All Levels


 4.7 (176,320)
Become an expert at SQL! | By Jose Portilla

Explore Course

SQL provides you with an easy-to-understand interface, allowing you to communicate with your database efficiently.

sql database illustration

Getting started with using SQL


Download a SQL engine
To use SQL, you must have a database management application. You can find plenty of SQL engines available online for free such as MySQ
for beginners using SQL!). Once you download your chosen engine, you’ll be able to connect your database to the application. 

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.

A query will return data. 

Consider the top most commonly used SQL queries:

1. Creating a table (CREATE TABLE)


To create a brand new, empty table, you will use the SQL keyword “CREATE TABLE.” As an example, let’s look at the following SQL 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:

ID (Primary key) Name Age

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).

2. Inserting records in a table (INSERT INTO)


You can easily insert rows of data, or records, into a new or existing table. To do this, you will use the INSERT INTO command, with the SQL
the first part identifies the columns that will have data in them, and the second part defines the data to put into those columns. 

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. 

 Simply type the following SQL query:

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’);

The Complete The A


SQL B
Jose Portilla SQL
MalvikC
V
4.7   (176,320) 4.6   (9
Bestseller
2 Amy 26

3 Bob 27

4 Chris 28

5 Dan 26

3. Viewing all records from a table (SELECT)


The SELECT statement represents one of the simplest and most frequently used in SQL. It allows you to view all of the records from a table

To use the SELECT statement, simply type:

SELECT * FROM student;

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

4. Arranging the records in a table (ORDER BY)


You use the ORDER BY clause with your SELECT query to arrange the displayed results in a particular order. 

For example, if you type the following:

SELECT * FROM student ORDER BY age;

You will get the following return:

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.

This would look as follows:

SELECT * FROM student DESC ORDER BY age;

You will then get the following return:

ID Name Age

1 Alan 28

4 Chris 28

3 Bob 27

2 Amy 26

5 Dan 26

5. Viewing only selected records from a table (SELECT COUNT)


If a huge number of rows exist in a table, and you do not want all the records to fill your display screen, then SQL gives an option to view on
to do this, as seen below:

SELECT COUNT(1) FROM student;


SELECT id , name , MAX(age) FROM student;

You will get this return:

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:

SELECT sum(age)FROM student;

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

6. Deleting records from a table (DELETE)


To delete selected rows from a table, fire the following query:

DELETE FROM student WHERE name = ‘alan’;

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 Analysis   PostgreSQ

Database Design   Database

Data Science

ID Name Age

2 Amy 26

3 Bob 27

4 Chris 28

5 Dan 26

7. Changing data in existing records in a table (UPDATE)


The UPDATE clause allows you to change information for records that already exist in your table. Suppose you want to change the age of a
query:

UPDATE student SET age = 22 WHERE name = ‘amy’;

Note that if the column referenced contains characters (as the Name column does), you must specify the specific value within single quotat

After writing the query above, if you then fire:

SELECT * FROM student;

You will get the following table as output:

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.

You may fire the following query:

SELECT * FROM student WHERE name LIKE ‘kell%’;

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.

9. Using more than one condition in WHERE clause to retrieve record


To understand the requirement of using the SQL keyword, WHERE, let us first insert one more row in our table — we will insert a sixth row
as described above).

Our table now becomes as follows:

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:

SELECT * FROM student WHERE name = ‘dan’;

The output will then be as follows:

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

For example, if you fire:

SELECT * FROM student WHERE name = ‘dan’ AND age = 24;

You will get the following output:

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:

SELECT * FROM student WHERE name = ‘dan’ OR age > 25

Your output will be:

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. 

You might also like