0% found this document useful (0 votes)
5 views10 pages

SQL Part One

The document provides an introduction to SQL and databases, explaining the types of Database Management Systems (DBMS) such as Relational and Non-Relational DBMS. It covers SQL commands for creating, inserting, selecting, and joining tables, as well as using clauses like WHERE, GROUP BY, ORDER BY, and LIMIT to manipulate and retrieve data. Additionally, it outlines the stages of SQL query execution, including compiling, binding, optimizing, and executing.

Uploaded by

Dina Dwi Annisa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

SQL Part One

The document provides an introduction to SQL and databases, explaining the types of Database Management Systems (DBMS) such as Relational and Non-Relational DBMS. It covers SQL commands for creating, inserting, selecting, and joining tables, as well as using clauses like WHERE, GROUP BY, ORDER BY, and LIMIT to manipulate and retrieve data. Additionally, it outlines the stages of SQL query execution, including compiling, binding, optimizing, and executing.

Uploaded by

Dina Dwi Annisa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SQL Introduction

Database : It is an Organized collection of data so that it can be easily


accessed.
To manage these databases, DBMS (Database Management System) are used.

Type Of DBMS : * Relational Database


* Non - Relational DBMS

Relation DBMS : In this DBMS, data strored in table format

row_id order_id order_date


4073 CA-2021-156104 06-12-2021
8179 CA-2022-166429 02-09-2022
2814 CA-2021-135685 08-16-2021
8573 CA-2020-121629 06-28-2020

For Example : MYSQL, Postgresql, oracle,

Relation DBMS : In this DBMS, data strore in key - vallue

{
"employee": {
"name": "sonoo",
"salary": 56000,
"married": true
}
}

For Example : MongoDB, Redis

RDBMS RDBMS
SQL
HOW SQL WORK

y Parser tree
Quer

Quer
it
Subm QUERY Parser ALgebrizer

y Pr
oces
s Tr
ee
Quer QUERY Execution Query Optimizer
y Re
sult
Execution Plan

The Complete Execution of a query is Categories into 3 main stages

Compiling (Parsing, Checks, and Semantics)


Binding
Optimizing
Executing

* Compiling
This is a part of compiling process, and in compiling parsing the query
statement is tokenized into individual words with appropriate verbiage and
clauses. The Compiling Semitics checks the validation of the statement and
match it with the system's catalogue. This Compiling stage validates whether
the query is valid or not, it also validates the authority of user to execute
the statement.

Binding
It creates the corresponding binary representation for the entered query
statement. All the SQL server engines has this compiling state where the byte
code get generated.

Optimising
It Optimizes the best algorithm for the byte code. This feature is also known
as Query Optimizer or Relational Engine.

Execution: The DBMS executes the optimized query, performing operations such
as retrieving data, updating records, or creating tables.
Result: The DBMS returns the result of the query (e.g., retrieved data) or an
acknowledgment of the performed operation (e.g., data inserted successfully
SQL COMMAND

SQL Create Table COMMAND

query : CREATE TABLE table_name


(
column1_name datatype1 [optional_constraints],
column2_name datatype2 [optional_constraints],
...
PRIMARY KEY (one_or_more_columns)
);
SQL keywords are CASE INSENSITIVE, For Example :

CREATE TABLE Employee_information employee_id first_name last_name position salary


( 1 John Doe Manager 75000
employee_id INT PRIMARY KEY,
first_name VARCHAR(50), 2 Jane Smith Developer 65000
last_name VARCHAR(50), 3 Emily Jones Designer 60000
position VARCHAR(50), 4 Michael Brown Analyst 58000
salary DECIMAL(10, 2)
5 Jessica Davis Consultant 70000

SQL INSERT Table COMMAND

The INSERT INTO statement in SQL is used to add new records to a table
in a database. It is a fundamental command for data insertion and is used
to insert new data into tables.

Query : INSERT INTO table_name (column1, column2, column3)


VALUES ( value1, value2, value);

employee_id first_name last_name position salary


6 agus mahari Manager 75000

note,
table_name: name of the table.
column1, column2..: name of first column, second column.
value1, value2, value..: value of first column, second column,… for the new record

SQL SELECT Table COMMAND

SELECT Statement in SQL


The SELECT statement in SQL is used to fetch or retrieve data from a
database. It allows users to access the data and retrieve specific data based
on specific conditions. In this example, we will fetch all the fields from the
table Customer:
Query : SELECT * FROM Employee_information;

employee_id first_name last_name position salary


1 John Doe Manager 75000
2 Jane Smith Develope 65000
3 Emily Jones Designer 60000
4 Michael Brown Analyst 58000
5 Jessica Davis Consultan 70000
6 agus mahari Manager 75000

SQL FROM Table COMMAND

Explanation:

FROM Employee_information: Specifies that the data will be


retrieved from the Employee_information table.

SQL WHERE Table COMMAND

WHERE keyword is used for fetching filtered data in a result set. It is used
to fetch data according to particular criteria.

Query : SELECT column1,column2 FROM table_name WHERE


column_name operator value;

Example : SELECT first_name, last_name FROM Employee_information


WHERE salary > 50000;

employee_id first_name last_name position salary


1 John Doe Manager 75000
2 Jane Smith Develope 65000
3 Emily Jones Designer 60000
4 Michael Brown Analyst 58000
5 Jessica Davis Consultan 70000

Explanation:
WHERE: The keyword used to specify the filter condition. salary > 50000:
The condition that must be met by the rows of data. Only rows where the
salary is greater than 50,000 will be displayed.
SQL JOIN Table COMMAND

Left Right Left Right Left Right Left Right


Table Table Table Table Table Table Table Table

SQL JOIN clause is used to query and access data from multiple tables by
establishing logical relationships between them. It can access data from
multiple tables simultaneously using common key values shared across
different tables.

SQL JOIN Example


Consider the two tables below as follows
Orders

order_id customer_id order_date amount


1 101 2023-01-10 100
2 102 2023-01-15 150
3 103 2023-01-20 200
4 104 2023-01-25 250
5 105 2023-01-30 300

Customers
customer_id customer_nam city
101 John Doe New York
102 Jane Smith Chicago
103 Emily Davis Los Angeles
106 Michael Brown Houston

Types of JOIN in SQL


There are many types of Joins in SQL. Depending on the use case, you can
use different type of SQL JOIN clause. Here are the frequently used SQL
JOIN types:
* INNER JOIN
* LEFT JOIN
* RIGHT JOIN
* FULL JOIN
The syntax for SQL INNER JOIN is:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 Left Right
INNER JOIN table2 Table Table

ON table1.matching_column =
table2.matching_column;

Example :
SELECT o.order_id, o.order_date, o.amount, c.customer_name, c.city
FROM Orders o
INNER JOIN Customers c ON o.customer_id = c.customer_id;

order_id order_date amount customer_name city


1 2023-01-10 100 John Doe New York
2 2023-01-15 150 Jane Smith Chicago
3 2023-01-20 200 Emily Davis Los Angeles

SQL LEFT JOIN


LEFT JOIN returns all the rows of the table on the left side of the join and
matches rows for the table on the right side of the join. For the rows for
which there is no matching row on the right side, the result-set will contain
null. LEFT JOIN is also known as LEFT OUTER JOIN.
The syntax for SQL LEFT JOIN is:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 Left Right
LEFT JOIN table2 Table Table
ON table1.matching_column = table2.matching_column;

Example :
SELECT o.order_id, o.order_date, o.amount, c.customer_name, c.city
FROM Orders o
LEFT JOIN Customers c ON o.customer_id = c.customer_id;
order_id order_date amount customer_name city
1 2023-01-10 100 John Doe New York
2 2023-01-15 150 Jane Smith Chicago
3 2023-01-20 200 Emily Davis Los Angeles
4 2023-01-25 250 NULL NULL
5 2023-01-30 300 NULL NULL
SQL RIGHT JOIN
RIGHT JOIN returns all the rows of the table on the right side of the join and
matching rows for the table on the left side of the join.It is very similar to
LEFT JOIN For the rows for which there is no matching row on the left
side, the result-set will contain null. RIGHT JOIN is also known as RIGHT OUTER
JOIN
The syntax for SQL RIGHT JOIN is:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 Left Right
Table Table
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

Example :
SELECT o.order_id, o.order_date, o.amount, c.customer_name, c.city
FROM Orders o
RIGHT JOIN Customers c ON o.customer_id = c.customer_id;

order_id order_date amount customer_name city


1 2023-01-10 100 John Doe New York
2 2023-01-15 150 Jane Smith Chicago
3 2023-01-20 200 Emily Davis Los Angeles
NULL NULL NULL Michael Brown Houston

SQL FULL OUTER JOIN


FULL JOIN creates the result-set by combining results of both LEFT JOIN
and RIGHT JOIN. The result-set will contain all the rows from both tables. For
the rows for which there is no matching, the result-set will contain NULL
values.
The syntax for SQL FULL OUTER JOIN is:

SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student Left Right
Table Table
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;

Example :
SELECT o.order_id, o.order_date, o.amount, c.customer_name, c.city
FROM Orders o
FULL JOIN Customers c ON o.customer_id = c.customer_id;
order_id order_date amount customer_name city
1 2023-01-10 100 John Doe New York
2 2023-01-15 150 Jane Smith Chicago
3 2023-01-20 200 Emily Davis Los Angeles
4 2023-01-25 250 NULL NULL
5 2023-01-30 300 NULL NULL
NULL NULL NULL Michael Brown Houston

SQL Group BY COMMAND

The GROUP BY Statement in SQL is used to arrange identical data into


groups with the help of some functions. i.e. if a particular column has the
same values in different rows then it will arrange these rows in a group.

- GROUP BY clause is used with the SELECT statement.


- In the query, the GROUP BY clause is placed after the WHERE clause.
- In the query, the GROUP BY clause is placed before the ORDER BY clause if
used.
Query : SELECT column1, function_name(column2)FROM table_name
WHERE condition
GROUP BY column1, column2

Example : SELECT customer_id, SUM(amount) AS total_amount


FROM Orders GROUP BY customer_id;

customer_id total_amount
101 100
102 150
103 200
104 250
105 300

Explanation:
SELECT customer_id, SUM(amount) AS total_amount: Selects the
customer_id column and calculates the total amount for each customer.
FROM Orders: Specifies the Orders table as the data source.
GROUP BY customer_id: Groups rows by the customer_id column.
SQL ORDER BY Table COMMAND

The ORDER BY statement in SQL is used to sort the fetched data in either
ascending or descending according to one or more columns. It is very useful
to present data in a structured manner.

SQL ORDER BY default mode is sorting data into ascending order. To sort
data in descending order use the DESC keyword with ORDER BY clause.

Query : SELECT * FROM table_name ORDER BY column_name ASC |


DESC

Example : SELECT customer_id, SUM(amount) AS total_amount


FROM Orders
GROUP BY customer_id
ORDER BY total_amount DESC;

customer_id total_amount
105 300
104 250
103 200
102 150
101 100

Explanation:
SELECT customer_id, SUM(amount) AS total_amount: Selects the customer_id
column and calculates the total amount for each customer.
FROM Orders: Specifies the Orders table as the data source.
GROUP BY customer_id: Groups rows by the customer_id column.
ORDER BY total_amount DESC: Sorts the results based on the total_amount
column in descending order.

SQL SELECT Table COMMAND

The LIMIT clause in SQL allows users to control the amount of data
retrieved and displayed in the result set.
It is useful when only a subset of records is needed for analysis or display
purposes in large databases with thousands of records.

Query : SELECT column1, column2, FROM table_name


LIMIT
Example : SELECT order_id, customer_name, product_name, order_date,
amount
FROM Orders
LIMIT 3;

order_id customer_nameproduct_name order_date amount


1 Alice Laptop 2024-01-15 1200
2 Bob Smartphone 2024-02-20 800
3 Charlie Tablet 2024-03-10 600

Explanation:
LIMIT 3: Limits query results to only display the first 3 rows.

You might also like