You are on page 1of 25

SQL FUNDAMENTALS

DATABASES
What are databases?
Why use databases?
Databases are systems that allow users to store and organise data.

They are useful when dealing with large amounts of data


Well databases actually have a wide variety of users.
These can include analysts such as a marketing analyst,
a business analyst or a sales or sales operation analyst;
or more technical people like data scientists,
software engineers, or web developers.
Basically anyone needing to deal with data
has a great use case to work with a database.
EVOLUTION FROM SPREADSHEET
TO DATABASE
• Spreadsheets • Databases
1. One time analysis 1. Data Integrity
2. Reasonable data set size 2. Can handle massive amounts of
3. Untrained people can work with data
data 3. Quickly combine different
datasets
4. Automate steps for reuse
5. Can support data for website and
applications
DATABASE PLATFORM OPTIONS
PostgreSQL Free(Open source)
Widely used on
internet
Multiplatform
MySQL • Free(Open source)
Maria SQL • Widely used on
internet
• Multiplatform
MS SQL Server Express • Free, but
compatible with
SQL Server
windows only
Microsoft Access • Cost
• Not easy to use SQL
SQLite • Free(Open source)
• Mainly command
line
• PostgreSQL is a great database choice for learning how to use SQL.
• PostgreSQL is an open source relational database management system
(DBMS) developed by a worldwide team of volunteers. PostgreSQL is not
controlled by any corporation or other private entity and the source code is
available free of charge.
• SQL learned in this course can be applied to a variety of databases or SQL
based software.
• SQL is the programming language used to communicate with our database
SQL IS USEFUL FOR A LOT OF
THINGS
• MySQL
• PostgreSQL
• Oracle Databases
• Microsoft Access
• Amazon’s Redshift
• Periscope Data
• Hive
• Google’s Big query
• Facebook’s presto
POSTGRESQL INSTALLATION AND
SETUP
• Objectives
1. Install PostgreSQL
2. Install PgAdmin
3. Restore Database
4. Perform Example Query
POSTGRESQL INSTALLATION AND
SETUP
• PgAdmin
1. Graphical Interface we will use to make queries
2. There are two versions 3.x and 4.x
3. PgAdmin 4 opens in the browser, PgAdmin 3 opens as a program
POSTGRESQL INSTALLATION AND
SETUP
• PostgreSQL
• The actual SQL engine that stores the data and is queried
• We will connect PostgreSQL Server to PgAdmin and then restore a database
to that server
POSTGRESQL INSTALLATION AND
SETUP
• Download PgAdmin From www.pgadmin.org

• Download PostgreSQL from www.postgresql.org


SELECT STATEMENT

• First, you specify a lost of columns in the table from which you want to query
data in the SELECT clause. You use a comma between each column in case
you want to query data from multiple columns.
• If you want to query data from all column, you can use an asterisk (*) as the
shorthand for all columns.
• Second, you indicate the table name after the FROM keyword.
• SQL Language is case insensitive
• Ideally, we should use SQL keywords in uppercase to make the code easier
to read and stand out clearly
• It's usually not good practise to use an asterisk in the SELECT statement
SELECT STATEMENT
• If you say SELECT asterisk, it's going to make your database server work a lot
harder and it's also going to increase the traffic between the database
server and its applications And as a result, it's usually going to slow down
whatever application is dependent on that database. Therefore, you should
usually specific the column names in the SELECT clause whenever possible to
get only necessary data from a table.
SELECT DISTINCT STATEMENT
• In a table, a column may contain many duplicate values ; and sometimes
you only want to list the different values.
• The DISTINCT keyword can be used to return only distinct values.
SELECT WHERE STATEMENT
• The WHERE clause appears right after the FROM clause of the SELECT
statement.
• The conditions are used to filter the rows returned from the SELECT statement.
• PostgreSQL provides you with various standard operators
• to construct the conditions.
COUNT STATEMENT
• The COUNT(*) function returns the number of rows returned by a SELECT
clause.
• When you apply the COUNT(*) to the entire table, PostgreSQL actually scans
the table sequentially.
• You can also specify a specific column count for readability – “SELECT
COUNT(column) FROM the table”
• Use the COUNT with DISTINCT- “SELECT COUNT (DISTINCT column) FROM
table” which will count the number of distinct rows for that particular
column.
LIMIT STATEMENT
• LIMIT allows you to limit the number of rows you get back after a query
• Useful when you want to get all the columns but not all the rows
• Goes at the end of a query.
ORDER BY STATEMENT
• When you query data from a table, PostgreSQL returns the rows in the order that
they were inserted into the table.
• In order to sort the result set, you can use the ORDER BY clause in conjunction with
the SELECT statement.
• The ORDER BY clause allows you o sort the rows returned from the SELECT statement
either ascending or descending order based on the criteria specified.
• Specify the column that you want to sort in the ORDER BY clause. If you sort the
resulting set by multiple columns, use a comma to separate between two columns.
• Use ASC to sort the resulting set in ascending order and DESC to sort the resulting set
in descending order
• If you leave it blank, the ORDER BY clause will use ASC by default
BETWEEN STATEMENT
• We use the between operator to match a value against a range of values.
• If the value is greater than or equal to the low value and less than or equal
to the high value, the expression returns true or vice versa.
• We can rewrite the between operator by using the greater than or equal
to(>=) or less than or equal to(<=) operators as the following statement.
Value >=l ow and value <= high;
IN STATEMENT
• You can use IN operator with the WHERE clause to check if a value matches
any value in a list of values.
• The syntax of an IN operator is: value IN (value1,value2,…)
• The expression returns true if the value matches any value in the list i.e., value
1, value 2, etc. The list of values is not limited to a list of numbers or strings but
also a result of a set of SELECT statement as – value in (select value from
table_name)
LIKE STATEMENT
• You construct a pattern by combining a string with wildcard characters and
use the LIKE or NOT LIKE operator to find the matches,
- Percent (%) for matching any sequence of characters
- Underscore ( _ ) for matching any single character
MIN MAX AVG SUM
AGGREGATE FUNCTIONS
GROUP BY STATEMENT
• The group by clause divides the rows returned from the SELECT statement
into groups.
• For each group, you can apply an aggregate function, for example:
- calculating the sum of items
- count the number of items in the group
• Group By Syntax:
SELECT column_1,aggregate_function(column_2) FROM table_name GROUP
BY column_1;
HAVING CLAUSE
• We often use that having clause in conjunction with the group by clause to
filter group rows that do not satisfy a specified condition.
• HAVING Syntax:
SELECT column_1,aggregare_function(column_2) FROM table_name GROUP
BY column_1 HAVING condition;
• The having clause sets the condition for group rows created by the GROUP
BY clause after the GROUP BY clause applies while the WHERE clause sets the
condition for individual rows before the group by clause applies.
• This is the main difference between the having and the where clauses.

You might also like