Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

MODULE 5: LESSON 2

Kindergarten of SQL
Summary of Last Class
Database - a collection of data typically stored in tables
Types of Database - Relational, NoSQL, Hierarchical, Cloud Database etc
Advantages of Database
1. More Storage e.g Big data
2. Many people can use it at once
3. Can be secured with encryption etc
SQL means Structured Query Language
SQL Flavours -
SQL Commands
SQL Nomenclature; Columns - Field, Row - Record, RDBMS
Data types
CONNECT TO SERVER
OBJECT EXPLORER OVERVIEW

RESTORE DATABASE
... LET'S EXPLORE THE RESTORED SAMPLE DATABASE

Database Diagram

Tables

Normalization

Normalization is a database design technique that reduces data redundancy and


eliminates undesirable characteristics.

Normalization rules divide larger tables into smaller tables and link them using
relationships. The purpose of Normalisation in SQL is to eliminate redundant
(repetitive) data and ensure data is stored logically.
SQL SYNTAX
SELECT & FROM STATEMENT
* , TOP, DISTINCT

AGGREGATE FUNCTIONS - Summarization


COUNT, MAX, MIN, AVG, SUM

COMMENT
--
/* & */
MULTIPLE DATABASE
e.g KCC.dbo.Customeers
SELECT + FROM
SELECT statement is used to retrieve data from a database table or tables.
The basic syntax of the SELECT statement in MSSQL is as follows:

SELECT *
FROM table_name;

SELECT column1, column2, ... SELECT DISTINCT column1


FROM table_name; FROM table_name;

SELECT TOP(n) column1 SELECT TOP (n) *


FROM table_name; FROM table_name;

NOTE: DISTINCT function is used to remove duplicate rows from the result set of a SELECT query. It returns only
unquie values
SUMMARIZATION DATA

NUMERICAL FIELDS ONLY


AVG () SELECT COUNT(*) AS total_employees
FROM employees;
SUM ()
SELECT COUNT(DISTINCT department)
AS total_departments
VARIOUS DATA TYPE FROM employees;
COUNT ()

MIN () & MAX ()


SQL SYNTAX

ALIASING - AS clause

FILTERING
WHERE clause
TEXT - LIKE & NOT LIKE, WILDCARDS ( %, _ )

MULTIPLE CRITERIA
OR, AND, BETWEEN, IN

NULL VALUES
IS NULL & IS NOT NULL
ALIASING
The AS keyword is used to provide an alias or alternative name for a column or a
table in the SELECT statement. You can also alias by adding a space

SELECT column_name AS alias_name


FROM table_name;

SELECT first_name + ' ' + last_name AS full_name


FROM employees;

SELECT column_name
FROM table_name AS alias_name;
FILTERING DATA
WHERE CLAUSE
The WHERE clause is used in the SELECT, UPDATE, and DELETE statements
to filter rows based on specified conditions. It allows you to retrieve, update,
or delete only those rows that meet the given criteria.

SELECT column1, column2, ...


FROM table_name
WHERE condition;

SELECT product_name
FROM products
WHERE category = 'Electronics' AND stock_quantity > 50;
COMPARISON OPERATORS
FILTERING TEXT
LIKE , NOT LIKE & WILDCARDS

Filters a pattern rather than a specific text

SELECT column 1, column 2


FROM table_name
WHERE column 1 LIKE pattern;
WILDCARDS
% - match zero. one or many characters including space

_ - match a single character

NOTE: Wildcards operations are case sensitive

Teaser - How would you write the wild card of a name that ends with 'e', has 't' as
the 3rd character, starts with 'A'
SQL SYNTAX

SORTING - ORDER BY, ASC,DESC

GROUPING - GROUP BY

QUERY EXECUTION ORDER


ORDER BY
The ORDER BY clause is used to sort the result set of a SELECT query based on specified
columns or expressions. It allows you to arrange the rows in either ascending or
descending order, depending on your requirements.

SELECT product_id, product_name, price


FROM products
ORDER BY price ASC;

SELECT product_id, product_name, price AS cash_money


FROM products
WHERE = price > 1000
GROUP BY (product_id)
ORDER BY cash_money DESC, product_name ASC;
GROUP BY
The GROUP BY clause is used in conjunction with aggregate functions to group rows
based on specified columns. It allows you to apply aggregate functions like SUM, COUNT,
AVG, MIN, MAX, etc., to subsets of data within each group. The GROUP BY clause is
commonly used to summarize data and perform calculations on grouped data.

SELECT column1, column2, ..., aggregate_function(column_name)


FROM table_name
GROUP BY column1, column2, ...;

SELECT customer_id, SUM(order_amount) AS total_order_amount


FROM orders
GROUP BY customer_id;
So you might be wondering how does this affect you It gives you a better
understanding of how SQL reads your script for example you can't se an alias made in
the SELECT statement in the WHERE clause but you can use it in the order by. Table
alias can be used everywhere because it is defined in then from statement
TEASER - Arrange the following Keyword in Order of Execution
INTRODUCTION TO DEBUGGING WITH CHAT GPT
COMMON ERRORS IN SQL

Misspelling

Incorrect Capitalization

Incorrect or Missing punctuation e.g missing


comma, apostrophe,bracket, etc

Keyword error
SAVING YOUR SCRIPT

You might also like