0% found this document useful (0 votes)
36 views

SQL For Beginners SQL Made Easy For Data Analysis

SQL is a programming language used to communicate with relational databases. SQL statements are used to query and manipulate data in these databases. The main clauses in SQL queries include SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. Common data manipulation functions include COUNT, SUM, AVG, MAX, MIN.

Uploaded by

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

SQL For Beginners SQL Made Easy For Data Analysis

SQL is a programming language used to communicate with relational databases. SQL statements are used to query and manipulate data in these databases. The main clauses in SQL queries include SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. Common data manipulation functions include COUNT, SUM, AVG, MAX, MIN.

Uploaded by

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

WHAT IS SQL?

SQL stands for Structured Query Language, which is a programming language


used to communicate with relational databases.
Relational Database Management System (RDBMS) is the basis for SQL.
PThe data in RDBMS is stored in database objects called tables. A table is a
collection of related data entries and it consists of rows and columns.
Each column of a table represents an attribute and each row in a table represents
a record. A row is a horizontal entity in a table and a column is a vertical entity
in a table.
RDBMS are built on Primary and Foreign Keys.
DIFFERENCES BETWEEN PRIMARY KEY AND FOREIGN KEY
PRIMARY KEY FOREIGN KEY
A primary key is used to ensure data A foreign key is a column or group
in the specific column is unique. of columns in a relational database
table that provides a link between
data in two tables.
It uniquely identifies a record in the It refers to the field in a table which
relational database table. is the primary key of another table.
Only one primary key is allowed in a More than one foreign key are
table. allowed in a table.
It is a combination of UNIQUE and It can contain duplicate values and a
Not Null constraints. table in a relational database.
It does not allow NULL. It can also contain NULL values.
Its value cannot be deleted from the It can be deleted from the table.
parent table.

LIST OF SQL STATEMENTS


SQL statements refer to very clear and simple “sentences” that we use to query a
database.
Data Definition Language (DDL):
CREATE database.
DROP (delete objects from the database).
ALTER the structure of the database.
TRUNCATE (remove all records from a table).
Data Manipulation Language (DML):
INSERT data into a table.
UPDATE existing data within a table.
DELETE records from a database table.
Data Control Language (DCL):
GRANT (gives users access privileges to the database.
REVOKE (withdraws the user’s access privileges).
Transaction Control Language (TCL):
COMMIT a transaction.
ROLLBACK a transaction in case of any error.
SAVEPOINT (sets a savepoint within a transaction).
SET TRANSACTION (specifies characteristics for the transaction).
Data Querying Language (DQL):
DQL statements are used for performing queries on data within the
schema objects.
It includes the SELECT statement.

SQL SYNTAX PRINCIPLES


Avoid the name of table/column in the plural. It’s better to use actor
instead of actors.
If the name of the table or column must consist of more than one word,
use an underscore to connect them, for example actor_id. You can also use
the CamelCase style, for example ActorID.
Avoid giving the same name to both a table and a column.
Include the AS keyword for creating aliases, because this makes the code
more readable.
Column names are case sensitive. “Name” is not the same as “name”
All SQL statements must contain at least: SELECT, a column name,
FROM, a table name.
SELECT statements are the beginning of every SQL query.
Do not retrieve more columns than you need (unnecessary use of
“SELECT*” in a query will likely return more columns than you need)
Specify the SELECT fields.

SQL SELECT STATEMENT


The SQL SELECT Statement is used to select data from a database.
The data returned is stored in a result table, called the result-set

Think of this scenario:


Your mum asked you to pick a fruit from the fridge.
She’s basically asking you to SELECT fruit FROM fridge.

SELECT syntax
SELECT column1, column 2, …
FROM table_name

Here, column 1, column 2,… are the field names of the table you
want to select data from. If you want to select all the fields from the
table, use the following syntax

SELECT * FROM table_name

Let’s look at the “actor” table below


The following SQL statement selects the first_name, last_name
from “actor table”

FROM CLAUSE
The FROM clause is required in a SELECT statement to identify the
tables that are being queried.
NOTE
When using the FROM clause in a SQL statement, there must be at least
one table listed in the FROM clause
If there are two or more tables listed in the SQL FROM clause, these
tables are generally joined using INNER OR OUTER JOINS.

WHERE CLAUSE
The WHERE clause is used to fetch data according to a particular criterion, it is
used to extract only those records that fulfil a specified condition
WHERE syntax
SELECT column 1, column 2, …
FROM table_name
WHERE condition;

OPERATORS IN THE WHERE CLAUSE


The following operators can be used in the WHERE clause
OPERATOR DESCRIPTION EXAMPLE
= Equal to SELECT * FROM actor
WHERE actor_Id = 2;
> Greater than SELECT first_name
FROM actor
WHERE actor_id>5;
< Less than SELECT * FROM actor
WHERE actor_id<12;
>= Greater than or equal to SELECT * FROM actor
WHERE actor_id >=5;
<= Less than or equal to SELECT * FROM actor
WHERE actor_id <= 12;
<> Not equal to. Note: In SELECT first_name,
some versions of SQL last_name FROM actor
this operator may be WHERE actor_id <>10;
written as ! =
BETWEEN Between a certain range SELECT first_name
FROM actor WHERE
actor_id BETWEEN 2 AND 7;
LIKE Search for a pattern SELECT first_name
FROM actor WHERE
last_name LIKE ‘s%’;
IN To specify multiple SELECT *FROM actor
possible values for a WHERE actor_id IN
column (‘2’,’3’,’4’);

ORDER BY AND GROUP BY


Both GROUP BY and ORDER BY are clauses that serve similar functions; that
is to sort query results. However, each of these serve very different purposes.
ORDER BY is used to sort the data in ascending or descending order. The
ORDER BY keywords sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.
GROUP BY is used to group rows that have same values into summary roles,
“like find the number of customers in each region”.
The GROUP BY is often used with aggregate functions.
ORDER BY AND GROUP BY Syntax
SELECT column 1, column 2, …
FROM table_name
GROUP BY column 1, column 2
ORDER BY column1, column 2 ASC/DESC;

SQL AGGREGATIONS
SQL provides aggregate functions to help with summarisation of large volumes
of data.
This function can produce a single value for an entire group or table.
They operate on sets of rows and return results based on groups of rows.

List of SQL Aggregate functions are:


Functions Description SYNTAX/EXAMPLE
SQL COUNT () The SQL COUNT function SELECT COUNT
returns the number of rows (column_name)
that matches a specified FROM table_name
criterion.
SQL SUM () The SQL SUM () function SELECT SUM
returns the total sum of a
(column_name)
numeric column. FROM table_name
SQL AVG () The SQL AVG () function SELECT AVG
returns the average value of a (column_name)
numeric column. FROM table_name
SQL MAX () The SQL MAX () function SELECT MAX
returns the largest-value (column_name)
element of a numeric column. FROM table_name
SQL MIN () The SQL MIN function SELECT MIN
returns the smallest-value (column_name)
element in the column. FROM table_name
SQL DISTINCT The SQL DISTINCT function SELECT DISTINCT
() is used to return only distinct (column_name)
values; it removes duplicates FROM table_name
values.

LIKE OPERATOR
The SQL LIKE operator is used in a WHERE clause to search for a specified
pattern in a column.
There are two wildcards often used in conjunction with the

LIKE operator:
The percent sign (%) represents zero, one or multiple characters.
The underscore sign (_) represents one, single character.
LIKE SYNTAX/EXAMPLE

Below are some examples showing different LIKE operators with ‘%’ and ‘_’
wildcards
ALIASING
SQL aliases are used to give a table, or column in a table, a temporary name.
Aliases are often used to make column names more readable.
An ALIAS is created with the AS keyword.

SYNTAX
SELECT column_name AS alias_name
FROM table_name

Demo Database
The following SQL statement creates an alias named “Total_sales
Aliases can be useful when:
There are more than one table involved in a query.
Functions are used in the query.
Column names are big or not very readable.
Two or more columns are combined together.
An SQL ALIAS is useful for simplifying your queries. It’s a temporary change
that does not affect the actual table name in the database. A temporary table
name can also be called a correlation name.

JOINS
The JOIN clause in SQL is used to combine rows from several tables based on a
related column between these tables.
INNER JOINS
This join type is used when we want to display matching records from two
tables.

Let’s say we want to show customers (i.e. the customer’s first name and last
name) along with their total amount. The amounts are stored in the payment
table, and the customers' names are stored in the customer table.
In our SQL query, we’ll join these two tables by matching the customer_id
column from payment table and the customer_id from customer table

CODE AND OUTPUT

SQL LEFT JOIN Keyword


The LEFT JOIN keyword returns all records from the left table(table1), and
matching records from the right table (table2). If there is no match, the result
will be NULL from the right side. In some databases, LEFT JOIN is called
LEFT OUTER JOIN
LEFT JOIN Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

SQL RIGHT JOIN Keyword


Right join is similar to LEFT JOIN. This join returns all the rows of table on the
right side of the join and matching rows for the table on the left side of the join.

RIGHT JOIN Syntax


SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name

SQL FULL JOIN Keyword


The SQL FULL JOIN is the result of combination of both left and right outer
join and the join table have all the records from both tables. It basically returns
all records from the left table and also from the right table. For example, let’s say
we have two tables, Table A and Table B. When FULL JOIN is applied on these
two tables, it returns all records from both Table A and Table B.
FULL JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name

NOTE: FULL OUTER JOIN can potentially return very large results-sets.
SQL CROSS JOIN Keyword
The CROSS JOIN keyword returns all records from both tables.

CROSS JOIN Syntax


SELECT column_name(s)
FROM table1
CROSS JOIN table2;
CONCLUSION
Throughout this book, we’ve seen SQL as a powerful programming language
that helps data analysts interact with data stored in Relational databases.
Knowing SQL is an important skill for data analysts.
We have covered some query examples to demonstrate the data analysis
capabilities of SQL.
Whether you’re running an online store or maintaining a game app, you almost
always have to store data in some way; that’s where SQL comes to play.

You might also like