Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

SQL

Uploaded by

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

SQL

Uploaded by

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

1.

Introduction to SQL
SQL (Structured Query Language) is a standard programming language specifically
designed for managing and manipulating databases.

2. SQL Basics
Database: An organized collection of data.
Table: A collection of related data entries consisting of rows and columns.
3. Data Definition Language (DDL)
Used to define and modify database structures:

CREATE: Creates a new table, view, or database.

sql
Copy code
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
ALTER: Modifies an existing database object.

sql
Copy code
ALTER TABLE table_name
ADD column_name datatype;
DROP: Deletes an entire table, view, or database.

sql
Copy code
DROP TABLE table_name;
4. Data Manipulation Language (DML)
Used to retrieve and manipulate data:

SELECT: Retrieves data from the database.

sql
Copy code
SELECT column1, column2, ...
FROM table_name
WHERE condition;
INSERT: Adds new data to the database.

sql
Copy code
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
UPDATE: Modifies existing data.

sql
Copy code
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE: Removes data from the database.

sql
Copy code
DELETE FROM table_name
WHERE condition;
5. Data Control Language (DCL)
Used to control access to data:

GRANT: Gives user access privileges.

sql
Copy code
GRANT SELECT, INSERT ON table_name TO user_name;
REVOKE: Removes user access privileges.

sql
Copy code
REVOKE SELECT, INSERT ON table_name FROM user_name;
6. Transaction Control Language (TCL)
Manages transactions in the database:

COMMIT: Saves the transaction.

sql
Copy code
COMMIT;
ROLLBACK: Undoes the transaction.

sql
Copy code
ROLLBACK;
SAVEPOINT: Sets a savepoint within a transaction.

sql
Copy code
SAVEPOINT savepoint_name;
7. Advanced SQL Concepts
Joins
Combines rows from two or more tables:

INNER JOIN: Returns records with matching values in both tables.

sql
Copy code
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_field = table2.common_field;
LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and matched
records from the right table.

sql
Copy code
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_field = table2.common_field;
RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and
matched records from the left table.

sql
Copy code
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_field = table2.common_field;
FULL JOIN (or FULL OUTER JOIN): Returns all records when there is a match in either
table.

sql
Copy code
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_field = table2.common_field;
Subqueries
A query within another query:

Scalar Subquery: Returns a single value.

sql
Copy code
SELECT column_name
FROM table_name
WHERE column_name = (SELECT MAX(column_name) FROM table_name);
Correlated Subquery: Depends on the outer query for its values.

sql
Copy code
SELECT column_name
FROM table_name_outer AS outer
WHERE column_name = (SELECT column_name FROM table_name_inner AS inner WHERE
outer.column_name = inner.column_name);
Indexes
Improves the speed of data retrieval operations:

CREATE INDEX: Creates an index on a table.


sql
Copy code
CREATE INDEX index_name
ON table_name (column1, column2, ...);
Views
Virtual tables based on the result set of a SQL query:

CREATE VIEW: Creates a view.


sql
Copy code
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Stored Procedures and Functions
Encapsulates a set of SQL queries for reuse and security:

Stored Procedure:

sql
Copy code
CREATE PROCEDURE procedure_name AS
BEGIN
-- SQL statements
END;
Function:

sql
Copy code
CREATE FUNCTION function_name (@parameter datatype)
RETURNS return_datatype AS
BEGIN
-- SQL statements
RETURN value;
END;
8. Common SQL Functions
Aggregate Functions
COUNT: Counts the number of rows.

sql
Copy code
SELECT COUNT(column_name)
FROM table_name;
SUM: Calculates the sum.

sql
Copy code
SELECT SUM(column_name)
FROM table_name;
AVG: Calculates the average.

sql
Copy code
SELECT AVG(column_name)
FROM table_name;
MIN/MAX: Finds the minimum/maximum value.

sql
Copy code
SELECT MIN(column_name), MAX(column_name)
FROM table_name;
String Functions
UPPER/LOWER: Converts to upper/lower case.

sql
Copy code
SELECT UPPER(column_name), LOWER(column_name)
FROM table_name;
CONCAT: Concatenates strings.

sql
Copy code
SELECT CONCAT(column1, column2)
FROM table_name;
SUBSTRING: Extracts a substring.

sql
Copy code
SELECT SUBSTRING(column_name, start, length)
FROM table_name;
Date Functions
GETDATE: Gets the current date and time.
sql
Copy code
SELECT GETDATE();
DATEADD: Adds a time interval to a date.

sql
Copy code
SELECT DATEADD(day, 10, column_name)
FROM table_name;
DATEDIFF: Calculates the difference between two dates.

sql
Copy code
SELECT DATEDIFF(day, column1, column2)
FROM table_name;
9. Normalization
The process of organizing data to minimize redundancy:

1NF (First Normal Form): Eliminate duplicate columns from the same table.
2NF (Second Normal Form): Remove subsets of data that apply to multiple rows of a
table and place them in separate tables.
3NF (Third Normal Form): Remove columns not dependent on the primary key.
10. SQL Performance Tuning
Techniques to improve the performance of SQL queries:

Use indexes appropriately.


Optimize joins.
Avoid subqueries in favor of joins when possible.
Use EXPLAIN or QUERY PLAN to understand query execution.
Normalize tables, but not to the extent of complicating query structures
excessively.
11. SQL Security
Ensuring the database is protected from unauthorized access and threats:

Implement user roles and permissions.


Use encryption for sensitive data.
Regularly audit and monitor database activities.
Backup and restore processes.
12. Advanced Topics
Triggers
Automates the execution of SQL code based on certain events:

CREATE TRIGGER:
sql
Copy code
CREATE TRIGGER trigger_name
AFTER INSERT ON table_name
FOR EACH ROW
BEGIN
-- SQL statements
END;
CTE (Common Table Expressions)
Temporary result sets used within a SELECT, INSERT, UPDATE, or DELETE statement.

WITH CTE:
sql
Copy code
WITH CTE AS (
SELECT column1, column2
FROM table_name
WHERE condition
)
SELECT *
FROM CTE;
Window Functions
Perform calculations across a set of table rows related to the current row.

ROW_NUMBER:
sql
Copy code
SELECT column1, ROW_NUMBER() OVER (PARTITION BY column2 ORDER BY column3) AS
row_num
FROM table_name;
This comprehensive overview covers the essential concepts and advanced topics in
SQL. Each topic is crucial for understanding how to effectively manage and
manipulate data within a relational database.

You might also like