0% found this document useful (0 votes)
274 views44 pages

SQL Presentation DATAwithBARAA

The document provides an overview of SQL concepts including: - SQL statement structure with clauses like SELECT, FROM, WHERE - Logical operators like AND, OR, and NOT - Comparison operators used in the WHERE clause like =, <, >, BETWEEN - Logical concepts for operators like AND requires both conditions to be true, OR requires one to be true - Functions like IN that check if a value is present in a list

Uploaded by

Slice Dice
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)
274 views44 pages

SQL Presentation DATAwithBARAA

The document provides an overview of SQL concepts including: - SQL statement structure with clauses like SELECT, FROM, WHERE - Logical operators like AND, OR, and NOT - Comparison operators used in the WHERE clause like =, <, >, BETWEEN - Logical concepts for operators like AND requires both conditions to be true, OR requires one to be true - Functions like IN that check if a value is present in a list

Uploaded by

Slice Dice
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/ 44

Homepage-DataWithBaraa

Youtube-DataWithBaraa

SQL No SQL
Key-Value Graph Store
Key Value
Table Key Value
Key Value
Key Value

Table
Column Store Document
Column Column

Values Values
Homepage-DataWithBaraa
Youtube-DataWithBaraa

SELECT Database
DBMS FROM
WHERE

Users SELECT.. Customers


FROM ..
Orders

Result

APPS
Homepage-DataWithBaraa
Youtube-DataWithBaraa

Library = Database

Book = Table

Category =Schema

Public Library Stuttgart


Homepage-DataWithBaraa
Youtube-DataWithBaraa

Server

Database/Schema Database/Schema

Table Table Table

Column Column Column Column Column Column


Homepage-DataWithBaraa
Youtube-DataWithBaraa
Homepage-DataWithBaraa
Youtube-DataWithBaraa

Column
Primary Key
Attribute
Key Field
Field

Row
Table
Record Object
Tuple Entity
Relation

Cell
Data Item
Column Value
Homepage-DataWithBaraa
Youtube-DataWithBaraa

SQL Command Types

DDL DQL DML DCL TCL


Data Definition Data Query Data Manipulation Data Control Transaction
Language Language Language Language Control Language

CREATE SELECT INSERT GRANT COMMIT

DROP DELETE REVOKE ROLLBACK

ALTER UPDATE SAVEPOINT


Homepage-DataWithBaraa
Youtube-DataWithBaraa

-- Select customer data

SELECT first_name, last_name

FROM customers

WHERE country = 'Italy‘


AND score < 500
Homepage-DataWithBaraa
Youtube-DataWithBaraa SQL Statement/Query

-- Select customer data

SELECT first_name, last_name

FROM customers

WHERE country = 'Italy‘


AND score < 500
Homepage-DataWithBaraa
Youtube-DataWithBaraa

Comment -- Select customer data

SELECT first_name, last_name

FROM customers

WHERE country = 'Italy‘


AND score < 500
Homepage-DataWithBaraa
Youtube-DataWithBaraa

-- Select customer data

SELECT first_name, last_name

Clauses FROM customers

WHERE country = 'Italy‘


AND score < 500
Homepage-DataWithBaraa
Youtube-DataWithBaraa

-- Select customer data

SELECT first_name, last_name

Keywords FROM customers

WHERE country = 'Italy‘


AND score < 500
Homepage-DataWithBaraa
Youtube-DataWithBaraa

-- Select customer data

SELECT first_name, last_name

FROM customers Identifiers

WHERE country = 'Italy‘


AND score < 500
Homepage-DataWithBaraa
Youtube-DataWithBaraa

-- Select customer data

SELECT first_name, last_name

FROM customers

WHERE country = 'Italy‘


AND score < 500

Operators
Homepage-DataWithBaraa
Youtube-DataWithBaraa SQL Statement/Query

Comment -- Select customer data

Clauses SELECT first_name, last_name

FROM customers Identifiers


Keywords
WHERE country = 'Italy‘
AND score < 500

Operators
Homepage-DataWithBaraa
Youtube-DataWithBaraa

SELECT Statement
SELECT DISTINCT
SELECT country,
COUNT(c.customer_id) AS total_customers

FROM FROM customers c

INNER JOIN orders o


JOINS ON o.customer_id = c.customer_id

WHERE WHERE country = 'germany'

GROUP BY GROUP BY c.country

HAVING HAVING COUNT(c.customer_id) > 1

ORDER BY ORDER BY c.country

LIMIT LIMIT 2
Homepage-DataWithBaraa
Youtube-DataWithBaraa Logical Operators AND OR

Condition A Condition B A AND B A OR B

TRUE TRUE TRUE TRUE

TRUE FALSE FALSE TRUE

FALSE TRUE FALSE TRUE

FALSE FALSE FALSE FALSE


Homepage-DataWithBaraa
Youtube-DataWithBaraa

Logical Operators NOT

Condition A NOT Condition A


TRUE FALSE

FALSE TRUE
Homepage-DataWithBaraa
Youtube-DataWithBaraa
WHERE Operators
Homepage-DataWithBaraa
Youtube-DataWithBaraa WHERE Operators
Comparison Operators Logical Operators

= Equal AND Return True if both conditions are true

!= <> Not Equal OR Return True if one of conditions is true

> Greater than NOT Reverse the result of any Boolean operator

< Less than IN Return True if a value is in a set of values

Return True if a value falls within a specific


=> Greater than or equal to BETWEEN
range

<= Less than or equal to LIKE Return True if a value matches a pattern
Homepage-DataWithBaraa
Youtube-DataWithBaraa Logical Operators AND OR

Condition A Condition B A AND B A OR B

TRUE TRUE TRUE TRUE

TRUE FALSE FALSE TRUE

FALSE TRUE FALSE TRUE

FALSE FALSE FALSE FALSE


Homepage-DataWithBaraa
Youtube-DataWithBaraa

AND - Return True only if both conditions are true


Condition A Condition B A AND B

TRUE TRUE TRUE

TRUE FALSE FALSE

FALSE TRUE FALSE

FALSE FALSE FALSE


Homepage-DataWithBaraa
Youtube-DataWithBaraa

OR- Return True if one of conditions is true


Condition A Condition B A OR B

TRUE TRUE TRUE

TRUE FALSE TRUE

FALSE TRUE TRUE

FALSE FALSE FALSE


Homepage-DataWithBaraa
Youtube-DataWithBaraa

NOT - Reverse the result of any Boolean operator


Condition A NOT Condition A
TRUE FALSE

FALSE TRUE
Homepage-DataWithBaraa
Youtube-DataWithBaraa

BETWEEN- Return True if a value falls within a specific range

FALSE TRUE FALSE

MIN MAX
value value
Homepage-DataWithBaraa
Youtube-DataWithBaraa

IN - Return True if a value is in a list of values


Checklist

Value 1
Value in the List? Value 2
Value 3

YES NO
Value N

TRUE FALSE
Homepage-DataWithBaraa
Youtube-DataWithBaraa

LIKE - Return True if a value matches a pattern


• Percent % matches anything
• Underscore _ matches exactly one character

Pattern
Find names begins with ‘M’ M%
Find names ends with ‘n’ %n
Find names containing the‘r’ %r%
Find names containing the‘r’ at 3d position __r%
Homepage-DataWithBaraa
Youtube-DataWithBaraa

Find names begins with ‘M’

Pattern M%
Maria M aria
John J ohn
Georg G eorg
Martin M artin
Peter P eter
Homepage-DataWithBaraa
Youtube-DataWithBaraa

Find names ends with ‘n’

Pattern % n
Maria Mari a
John Joh n
Georg Geor g
Martin Marti n
Peter Pete r
Homepage-DataWithBaraa
Youtube-DataWithBaraa

Find names containing the‘r’

Pattern % r %
Maria Ma r ia
John John
Georg Geo r g
Martin Ma r tin
Peter Pete r
Homepage-DataWithBaraa
Youtube-DataWithBaraa

Find names containing the‘r’ at 3d position

Pattern __ r %
Maria Ma r ia
John Jo h n
Georg Ge o rg
Martin Ma r tin
Peter Pe t er
Homepage-DataWithBaraa
Youtube-DataWithBaraa SQL JOINS

Join Key
customer_id

Join Type
INNER Join
LEFT Join
RIGHT Join

FULL Join
Homepage-DataWithBaraa
Youtube-DataWithBaraa INNER JOIN

LEFT Table RIGHT Table


Only matching Rows Only matching Rows

Customers Orders
Homepage-DataWithBaraa
Youtube-DataWithBaraa LEFT JOIN

LEFT Table RIGHT Table


Return ALL Rows Only matching Rows

Customers Orders
Homepage-DataWithBaraa
Youtube-DataWithBaraa RIGHT JOIN

LEFT Table RIGHT Table


Only matching Rows Return ALL Rows

Customers Orders
Homepage-DataWithBaraa
Youtube-DataWithBaraa FULL JOIN

LEFT Table RIGHT Table


Return ALL Rows Return ALL Rows

Customers Orders
Homepage-DataWithBaraa
Youtube-DataWithBaraa

JOINS UNION

Col1 col2 Col1 col2 Col1 col2 Col1 col2

Rows Rows Rows Rows Rows Rows Rows Rows

Col1 col2 Col1 col2 Col1 col2

Rows Rows Rows Rows Rows Rows

Rows Rows
Homepage-DataWithBaraa
Youtube-DataWithBaraa

SQL Aggregate Functions

COUNT() Returns the number of rows in a result set

SUM() Returns the sum of values

AVG() Returns the average of values

MAX() Returns the maximum value

MIN() Returns the minimum value


Homepage-DataWithBaraa
Youtube-DataWithBaraa

SQL String Functions

CONCAT() Returns a string by concatenating two or more string values.

LOWER() Converts a string to lowercase.

UPPER() Converts a string to uppercase.


Remove leading and trailing spaces
TRIM()
from a string.
LENGTH() Returns the length of a string

SUBSTRING() Returns a substring from string.


Homepage-DataWithBaraa
Youtube-DataWithBaraa

SQL Date Functions

NOW() Returns the current date and time.

DAY() Returns the day of the month (0-31).

MONTH() Returns the month from the date passed (1-12).

YEAR() Returns the year.

DATE_ADD() Adds a specified time value (or interval) to a date value.

DATE_SUB() Subtracts a specified time value (or interval) from a date value.

DATEDIFF() Returns the number of days between two dates


Homepage-DataWithBaraa
Youtube-DataWithBaraa

SUbqueries

Query

Result
Query 1 Query 2

(Subquery)
Homepage-DataWithBaraa
Youtube-DataWithBaraa TRIM

LTRIM()
Maria RTRIM()

LEFT RIGHT
SPACE SPACE
TRIM()
(LTRIM + RTRIM)
Homepage-DataWithBaraa
Youtube-DataWithBaraa SUBSTRING
SUBSTRING(Column,Start,Length)
SUBSTRING(‘Maria’,2,3)

Maria ari
1 2 3 4 5

Start

End
Homepage-DataWithBaraa

COLUMN Definition
Youtube-DataWithBaraa

Column Name Data Type Constraints

Could be INT PRIMARY KEY


Anything
VARCHAR NOT NULL

DATE UNIQUE

CHAR DEFAULT
… …

You might also like