SQL
SQL (Structured Query Language) Introduction
SQL is a standard programming language used to manage and manipulate relational databases. It is essential for querying, updating, and managing data within a database system.
SQL is used to interact with databases such as MySQL, PostgreSQL, SQL Server, SQLite, and others.
Key Concepts of SQL:
- Database: A collection of related data organized in tables.
- Table: A structure within a database that stores data in rows and columns.
- Row (Record): A single, data entry in a table (i.e., a record).
- Column (Field): A single data attribute that describes the data type and content of the table (e.g., Name, Date of Birth, etc.).
Common SQL Commands
SELECT - Retrieves data from the database.
- Example:
This query selects all rows and columns from theemployeestable.
- Example:
INSERT INTO - Adds new data to a table.
- Example:
This query adds a new employee record to theemployeestable.
- Example:
UPDATE - Modifies existing data in a table.
- Example:
This query updates the position of the employee withid1.
- Example:
DELETE - Removes data from a table.
- Example:
This query deletes the employee withid1 from theemployeestable.
- Example:
CREATE TABLE - Defines a new table and its columns.
- Example:
This query creates a new table calledemployeeswith columns forid,name,position, andhire_date.
- Example:
ALTER TABLE - Modifies the structure of an existing table (e.g., adding/removing columns).
- Example:
This query adds a new columnsalaryto theemployeestable.
- Example:
DROP TABLE - Deletes an entire table from the database.
- Example:
This query deletes theemployeestable from the database.
- Example:
WHERE: Filters records based on specified conditions.
- Example:
- Example:
ORDER BY: Sorts the result set by one or more columns.
- Example:
- Example:
AND / OR: Combines multiple conditions in the
WHEREclause.- Example:
- Example:
GROUP BY: Groups rows that have the same values into summary rows.
- Example:
- Example:
HAVING: Filters the results of
GROUP BY(similar toWHERE, but for grouped data).- Example:
- Example:
- INT: Integer numbers.
- VARCHAR: Variable-length character string (text).
- DATE: Date in 'YYYY-MM-DD' format.
- DECIMAL: Fixed-point numbers (useful for currency or precise calculations).
INNER JOIN: Returns records that have matching values in both tables.
- Example:
- Example:
LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table, and the matched records from the right table (returns NULL for non-matching rows in the right table).
- Example:
- Example:
RIGHT JOIN (or RIGHT OUTER JOIN): Similar to LEFT JOIN but returns all records from the right table.
FULL OUTER JOIN: Returns all records when there is a match in either left or right table.
SQL Clauses
Basic Data Types in SQL
SQL Joins
SQL joins allow you to combine data from two or more tables based on related columns.
Conclusion
SQL is a powerful language used for managing and manipulating relational databases. It’s essential for tasks such as retrieving, inserting, updating, and deleting data. By mastering basic SQL commands and concepts like joins, aggregations, and filtering, you'll be well-equipped to work with databases effectively.
Let me know if you need more detailed examples or have specific questions about SQL!
.jpg)
0 Comments