SQL For Beginners SQL Made Easy For Data Analysis
SQL For Beginners SQL Made Easy For Data Analysis
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
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;
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.
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
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.