My SQL
My SQL
- Vasanth S
What is a Database?
◦ “A database is an organized collection of
“ structured information, or data”
Why Database?
SQL
◦ SQL databases use structured query language and
have a pre-defined schema for defining and
manipulating data.
◦ It a safe choice for many use cases.
◦ It’s perfect for complex queries.
Non- Relational Database
◦ Table / Schema
◦ Rows / Records
◦ Columns / Fields
DDL – Data Definition Language
UPDATE table_name
SET column_name1 = new-value1,
column_name2=new-value2, ...
[WHERE Clause]
DELETE
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
BETWEEN Operator
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
ORDER BY Keyword
SELECT column_list
FROM table_name
LIMIT count;
AGGREGATE FUNCTIONS
◦ Syntax:
SELECT SUM(aggregate_expression)
FROM tables
[WHERE conditions];
COUNT Function
SELECT AVG(aggregate_expression)
FROM tables
[WHERE conditions];
MIN() and MAX() functions
The INNER JOIN keyword selects records that have matching values in
both tables.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
LEFT JOIN
The LEFT JOIN keyword returns all records from the left table
(table1), and the matching records from the right table (table2).
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
RIGHT JOIN
The RIGHT JOIN keyword returns all records from the right table (table2),
and the matching records from the left table (table1).
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
FULL JOIN
The FULL OUTER JOIN keyword returns all records when there is a
match in left (table1) or right (table2) table records.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
SELF JOIN
A self join is a regular join, but the table is joined with itself.
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;