SQL - Aamir P
Hello everyone!
Recently, I have started to revise SQL. I thought of also sharing my knowledge with you all.
SQL (Structured Query Language)?is a powerful tool for managing and querying relational databases. Let's begin with some foundational concepts and SQL commands:
What is SQL?
?Basic SQL Commands:
SELECT column1, column2 FROM table_name;
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
DELETE FROM table_name WHERE condition;
CREATE TABLE table_name (
??column1 datatype,
??column2 datatype,
??...
);
ALTER TABLE table_name ADD column_name datatype;
DROP TABLE table_name;
Data Types:
SQL supports various data types like INT, VARCHAR, DATE, etc., which define the kind of data a column can hold.
CREATE TABLE students (
??student_id INT,
??first_name VARCHAR(50),
??last_name VARCHAR(50),
??birthdate DATE
);
Constraints:
Constraints are rules applied to a column to enforce data integrity. Examples include PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, etc.
CREATE TABLE employees (
??employee_id INT PRIMARY KEY,
??first_name VARCHAR(50),
??last_name VARCHAR(50),
??hire_date DATE NOT NULL
);
Sorting and Filtering:
SELECT product_name, price FROM products ORDER BY price DESC;
(By default, it is ascending. DESC is mentioned to say descending)
SELECT customer_name, order_date FROM orders WHERE order_date >= '2023-01-01';
Joins:
1. INNER JOIN:
Example:?Suppose you have two tables, "employees" and "departments," and you want to retrieve a list of employees along with their department names.
SELECT employees.employee_name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;
领英推荐
2. LEFT JOIN (or LEFT OUTER JOIN):
Example:?You want to retrieve a list of all departments and the employees in each department.
SELECT departments.department_name, employees.employee_name
FROM departments
LEFT JOIN employees ON departments.department_id = employees.department_id;
3. RIGHT JOIN (or RIGHT OUTER JOIN):
Example:?You want to retrieve a list of all employees and their corresponding department names.
SELECT employees.employee_name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id;
4. FULL JOIN (or FULL OUTER JOIN):
Example:?You want to retrieve a list of all departments and all employees, including those without departments.
SELECT departments.department_name, employees.employee_name
FROM departments
FULL JOIN employees ON departments.department_id = employees.department_id;
5. SELF JOIN:
Example:?Suppose you have an "employees" table with a "manager_id" column, and you want to retrieve a list of employees and their respective managers.
SELECT e.employee_name AS employee, m.employee_name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;
Aggregation:
SQL provides functions like SUM, COUNT, AVG, MIN, and MAX to perform calculations on data.
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
Subqueries:
Subqueries are queries nested within other queries, often used for more complex filtering or calculations.
SELECT product_name, price
FROM products
WHERE category_id IN (SELECT category_id FROM categories WHERE category_name = 'Electronics');
GROUP BY and HAVING:??
SELECT department, COUNT(employee_id) AS num_employees
FROM employees
GROUP BY department;
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
Indexes:?
Indexes can improve query performance by speeding up data retrieval.
Transactions:?
SQL supports transactions to ensure data consistency and integrity.
So, that’s it for the day! Thanks for your time in reading my article. Tell me your feedback or views in the comments section.
Check out this link to know more about me
BDE-AWS at Tiger Analytics | 2x Apache Airflow,2xDatabricks certified | Python | SQL
1 年Cool Aamir Thanks for the quick revision.its helpful