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

Lesson-1-Introduction-to-SQL

The document provides an introduction to Structured Query Language (SQL), outlining its purpose, environment, and components such as Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL). It details the steps for creating tables, writing basic SQL queries, and using commands for inserting, updating, deleting, and selecting data. Additionally, it explains the use of clauses like ORDER BY, LIMIT, GROUP BY, HAVING, and DISTINCT in SQL queries.

Uploaded by

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

Lesson-1-Introduction-to-SQL

The document provides an introduction to Structured Query Language (SQL), outlining its purpose, environment, and components such as Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL). It details the steps for creating tables, writing basic SQL queries, and using commands for inserting, updating, deleting, and selecting data. Additionally, it explains the use of clauses like ORDER BY, LIMIT, GROUP BY, HAVING, and DISTINCT in SQL queries.

Uploaded by

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

Structured

Query
Language
Introduction
Learning Outcomes

▰ Define a database using SQL data


definition language
▰ Write queries using SQL

2
Purpose of SQL
Standard
▰ Specify syntax/semantics for data definition and
manipulation
▰ Define data structures and basic operations
▰ Enable portability of database definition and
application modules
▰ Allow for later growth/enhancement to standard
(referential integrity, transaction management,
user-defined functions, extended join operations,
national character sets)

3
SQL Environment
▰ Catalog
▻ A set of schemas that constitute the description of a database
▰ Schema
▻ The structure that contains descriptions of objects created by a user (base
tables, views, constraints)
▰ Data Definition Language (DDL)
▻ Commands that define a database, including creating, altering, and dropping
tables and establishing constraints
▰ Data Manipulation Language (DML)
▻ Commands that maintain and query a database
▰ Data Control Language (DCL)
▻ Commands that control a database, including administering privileges and
committing data

4
SQL Data Types

5
DDL, DML, DCL, and the database development process 6
6
SQL Database Definition
▰ Data Definition Language (DDL)
▰ Major CREATE statements:
▻ CREATE SCHEMA–defines a portion of the
database owned by a particular user
▻ CREATE TABLE–defines a new table and its
columns
▻ CREATE VIEW–defines a logical table from
one or more tables or views
▰ Other CREATE statements: CHARACTER SET,
COLLATION, TRANSLATION, ASSERTION, DOMAIN 7
Steps in Table
Creation
1. Identify data types for attributes
2. Identify columns that can and cannot be null
3. Identify columns that must be unique (candidate keys)
4. Identify primary key–foreign key mates
5. Determine default values
6. Identify constraints on columns (domain specifications)
7. Create the table and associated indexes

8
General syntax for CREATE TABLE statement used in
data definition language

9
Basic SQL
Queries
Defining how
Data Definition
your data is
Language
stored
• CREATE DATABASE
• DROP DATABASE
• USE
• CREATE TABLE
• ALTER TABLE
• DROP TABLE
• DESCRIBE
11
Manipulating Data Manipulation
your Data Language
• INSERT
• UPDATE
• DELETE
• SELECT
12
INSERT
Insert is used to insert a
one or more records into
a table.

13
Example:
Inserting into a table

Inserting a record that has some null attributes requires


identifying the fields that actually get data

Inserting from another table

1
DELETE
Removes rows from a
table.

15
Example:

 Delete certain rows


 DELETE FROM CUSTOMER_T WHERE
CUSTOMERSTATE = ‘HI’;
 Delete all rows
 DELETE FROM CUSTOMER_T;

1
UPDATE
Modifies data in existing
rows.

17
Example:

1
SELECT
SELECT is used to retrieve
rows selected from one
or more tables, and can
include UNION
statements and
subqueries.

19
• Each select_expr expression
indicates a column or data
that you want to retrieve.
• The FROM clause indicates the
SELECT table or tables from which to
retrieve rows.
• The WHERE clause, if given,
indicates the condition or
conditions that rows must
satisfy to be selected.
20
NOTE: In the WHERE clause, you can
use any of the functions and operators
that MariaDB supports, except for
aggregate (summary) functions

21
ORDER
BY
▪ Clause used to order the
results
▪ ASC (A-Z, 1 to N)
▪ DESC(Z-A, N to 1)

22
LIMIT
▪ Clause that allows you to
restrict the results to only a
certain number of rows,
optionally with an offset.

23
Example:

2
GROUP
BY
▪ Use the GROUP BY clause in a
SELECT statement to group rows
together that have the same
value in one or more column, or
the same computed value using
expressions
▪ may work with the clause
25
GROUP
BY
When you use a GROUP BY clause,
you will get a single result row for
each group of rows that have
the same value for the expression
given in GROUP BY.

26
Syntax:
SELECT expression1,
expression2, ... expression_n,
aggregate_function
(expression)
FROM tables [WHERE
conditions] GROUP BY
expression1, expression2, ...
2
Where:
aggregate_function
It can be a function such as SUM,
COUNT, MIN, MAX, or AVG
functions.

2
Example - Using SUM function

2
Example - Using COUNT function

3
Example - Using MIN function

3
Example - Using MAX function

3
HAVING
▪ HAVING clause is used in
combination with the
GROUP BY clause to restrict
the groups of returned rows
to only those whose the
condition is TRUE.
33
Syntax:

3
Example:

3
DISTINCT
▪ DISTINCT clause is used to
remove duplicates from
the results of a SELECT
statement.

36
Syntax:

3
Example:

3
Example:

You might also like