SQL - Aamir P
SQL - AAMIR P

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?

  • SQL is a programming language used for managing and querying relational databases.
  • It allows you to interact with databases to store, retrieve, update, and manipulate data.

?Basic SQL Commands:

  • SELECT:?Used to retrieve data from a database.

SELECT column1, column2 FROM table_name;


  • INSERT:?Used to insert new records into a table.

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


  • UPDATE:?Used to modify existing records in a table.

UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;


  • DELETE:?Used to delete records from a table.

DELETE FROM table_name WHERE condition;


  • CREATE TABLE:?Used to create a new table.

CREATE TABLE table_name (

??column1 datatype,

??column2 datatype,

??...

);


  • ALTER TABLE:?Used to modify an existing table.

ALTER TABLE table_name ADD column_name datatype;


  • DROP TABLE:?Used to delete an existing table.

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:

  • You can use the ORDER BY clause to sort query results.

SELECT product_name, price FROM products ORDER BY price DESC;

(By default, it is ascending. DESC is mentioned to say descending)

  • The WHERE clause allows you to filter data based on conditions.

SELECT customer_name, order_date FROM orders WHERE order_date >= '2023-01-01';


Joins:

  • Joins are used to combine data from multiple tables based on a related column.
  • Common types of joins include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.

1. INNER JOIN:

  • An INNER JOIN returns only the rows that have matching values in both tables.

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):

  • A LEFT JOIN returns all rows from the left table (the first table mentioned) and the matched rows from the right table (the second table mentioned). If there is no match, NULL values are returned for columns from the right table.

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):

  • A RIGHT JOIN is similar to a LEFT JOIN but returns all rows from the right table and the matched rows from the left table.

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):

  • A FULL JOIN returns all rows when there is a match in either the left or the right table. If there is no match, NULL values are returned for columns from the non-matching side.

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:

  • A SELF JOIN is used to join a table with itself. It is often used when a table has a hierarchical structure, such as an organizational chart.

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:??

  • GROUP BY is used to group rows based on a column.?

SELECT department, COUNT(employee_id) AS num_employees

FROM employees

GROUP BY department;


  • HAVING is used to filter grouped results.

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

https://www.dhirubhai.net/feed/update/urn:li:activity:7006538868665577472/


Shanmuka Venkatesh Ragam

BDE-AWS at Tiger Analytics | 2x Apache Airflow,2xDatabricks certified | Python | SQL

1 年

Cool Aamir Thanks for the quick revision.its helpful

要查看或添加评论,请登录

AAMIR P的更多文章

  • CPG (Consumer Packed Goods)— Aamir P

    CPG (Consumer Packed Goods)— Aamir P

    Hello Readers! In this article, we will gain some understanding about CPG. What is CPG? Things that are frequent in…

    1 条评论
  • Dataiku — Aamir P

    Dataiku — Aamir P

    I found this tool very interesting and thought of sharing it with you all. I learnt this from Dataiku Academy.

  • PySpark — Aamir P

    PySpark — Aamir P

    As part of my learning journey and as a requirement for my new project, I have started exploring Pyspark. In this…

  • Data Build Tool(DBT) — Aamir P

    Data Build Tool(DBT) — Aamir P

    This is a command-line environment that allows you to transform and model the data in data warehousing using SQL…

  • SSIS Data Warehouse Developer — Aamir P

    SSIS Data Warehouse Developer — Aamir P

    SQL Server is an RDBMS developed by Microsoft. It is used to store and retrieve data requested by apps.

    4 条评论
  • Talend — Aamir P

    Talend — Aamir P

    Hello Readers! In this article, we will learn about Talend. Data integration is crucial for businesses facing the…

  • Data Warehousing and BI Analytics — Aamir P

    Data Warehousing and BI Analytics — Aamir P

    Hello Readers! In this article, we will have a beginner-level understanding of Data Warehousing and BI Analytics. Hope…

  • TensorFlow - Aamir?P

    TensorFlow - Aamir?P

    Hi all! This is just some overview which I’m going to write about. Some beginners were asking me for a basic…

  • Data Engineering — Aamir P

    Data Engineering — Aamir P

    Hello readers! In this article, we will see a basic workflow of Data Engineering. Let's see how data is stored…

    2 条评论
  • SnowPark Python— Aamir P

    SnowPark Python— Aamir P

    Hello readers! Thank you for supporting all my articles. This article SnowPark Python I am not so confident because…

社区洞察

其他会员也浏览了