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

SQL_Basics

Uploaded by

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

SQL_Basics

Uploaded by

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

SQL Basics

1. What is SQL?

- SQL (Structured Query Language) is a standard language for managing and manipulating

relational databases.

2. Key Features of SQL:

- **Data Querying:** Retrieve data using SELECT statements.

- **Data Manipulation:** Insert, update, and delete records.

- **Data Definition:** Create, alter, and drop tables and other database objects.

- **Data Control:** Manage user access with GRANT and REVOKE.

3. Basic SQL Commands:

- **Data Query Language (DQL):**

SELECT column1, column2 FROM table_name WHERE condition;

- **Data Manipulation Language (DML):**

- INSERT INTO table_name (column1, column2) VALUES (value1, value2);

- UPDATE table_name SET column1 = value WHERE condition;

- DELETE FROM table_name WHERE condition;

- **Data Definition Language (DDL):**

- CREATE TABLE table_name (column1 datatype, column2 datatype);

- ALTER TABLE table_name ADD column_name datatype;

- DROP TABLE table_name;

4. SQL Clauses:

- **WHERE:** Filters records.


- **ORDER BY:** Sorts records.

- **GROUP BY:** Groups records based on column values.

- **HAVING:** Filters groups.

Example:

SELECT name, SUM(salary)

FROM employees

WHERE department = 'Sales'

GROUP BY name

HAVING SUM(salary) > 5000

ORDER BY SUM(salary) DESC;

5. SQL Joins:

- **INNER JOIN:** Retrieves records with matching values in both tables.

SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;

- **LEFT JOIN:** Retrieves all records from the left table and matching records from the right.

- **RIGHT JOIN:** Retrieves all records from the right table and matching records from the left.

- **FULL JOIN:** Retrieves all records when there is a match in either table.

6. SQL Functions:

- **Aggregate Functions:**

- COUNT(), SUM(), AVG(), MAX(), MIN()

- **String Functions:**

- CONCAT(), LENGTH(), LOWER(), UPPER(), SUBSTRING()

- **Date Functions:**

- NOW(), CURDATE(), YEAR(), MONTH(), DAY()


7. Constraints in SQL:

- **NOT NULL:** Ensures a column cannot have NULL values.

- **UNIQUE:** Ensures all values in a column are unique.

- **PRIMARY KEY:** Uniquely identifies a record in a table.

- **FOREIGN KEY:** Links two tables.

- **CHECK:** Ensures a condition is met for a column.

- **DEFAULT:** Provides a default value for a column.

8. Example Table Creation:

CREATE TABLE Employees (

ID INT PRIMARY KEY,

Name VARCHAR(50) NOT NULL,

Department VARCHAR(50),

Salary DECIMAL(10, 2) CHECK (Salary > 0)

);

9. Best Practices:

- Use meaningful table and column names.

- Normalize your database to reduce redundancy.

- Use proper indexing to improve query performance.

- Use transactions to maintain data integrity.

You might also like