Skip to content

Added SQL under DBMS under Docs #897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/DBMS/Structured Query Language/_category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "SQL",
"position": 8,
"link": {
"type": "generated-index",
"description": "Explore SQL in DBMS."
}
}
84 changes: 84 additions & 0 deletions docs/DBMS/Structured Query Language/sql-aggregate-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
id: sql-aggregate-function
title: DBMS - SQL Aggregate Functions
sidebar_label: Aggregate Functions
sidebar_position: 3
description: Learn about the SQL Aggregate Functions.
tags:
- DBMS
- SQL
- SQL-Functions
- Database Design
---

# DBMS - SQL Aggregate Functions

Aggregate functions in SQL are used to perform calculations on multiple rows of a table's column and return a single value. These functions are essential for data analysis and reporting as they help in summarizing large datasets.

## COMMON AGGREGATE FUNCTIONS

1. **COUNT():** The COUNT() function returns the number of rows that match a specified condition. This query returns the total number of rows in the table.
```sql
SELECT COUNT(*) AS total_rows
FROM table_name;
```

2. **SUM():** The SUM() function returns the total sum of a numeric column. This query calculates the sum of all values in column_name.
```sql
SELECT SUM(column_name) AS total_sum
FROM table_name;
```

3. **AVG():** The AVG() function returns the average value of a numeric column. This query calculates the average value of column_name.
```sql
SELECT AVG(column_name) AS average_value
FROM table_name;
```

4. **MIN():** The MIN() function returns the smallest value in a specified column. This query finds the smallest value in column_name.
```sql
SELECT MIN(column_name) AS minimum_value
FROM table_name;
```

5. **MAX():** The MAX() function returns the largest value in a specified column. This query finds the largest value in column_name.
```sql
SELECT MAX(column_name) AS maximum_value
FROM table_name;
```

## AGGREGATE FUNCTIONS WITH GROUP BY

Aggregate functions are often used in conjunction with the GROUP BY clause to group the result set by one or more columns and perform the calculation on each group.
```sql
SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department;
```
This query groups the employees by their department and returns the number of employees in each department.

```sql
SELECT department, COUNT(*) AS total_employees, AVG(salary) AS average_salary, MAX(salary) AS highest_salary
FROM employees
GROUP BY department;
```
This query groups the employees by their department and returns the total number of employees, average salary, and highest salary in each department.

## AGGREGATE FUNCTIONS USING HAVING

The HAVING clause is used to filter groups based on the result of aggregate functions. It is similar to the WHERE clause, but WHERE cannot be used with aggregate functions.
```sql
SELECT department, COUNT(*) AS total_employees
GROUP BY department
HAVING COUNT(*) > 10;
```
This query groups the employees by their department and returns the departments that have more than 10 employees.

You can combine multiple aggregate functions in a single query to perform various calculations.
```sql
SELECT COUNT(*) AS total_rows, SUM(column_name) AS total_sum, AVG(column_name) AS average_value
FROM table_name;
```
This query returns the total number of rows, the sum of column_name, and the average value of column_name.

Aggregate functions are powerful tools in SQL for summarizing and analyzing data. By mastering these functions, you can perform complex data analysis and gain valuable insights from your database.
71 changes: 71 additions & 0 deletions docs/DBMS/Structured Query Language/sql-basic-concepts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
id: sql-basic-concepts
title: DBMS - SQL Basic Concepts
sidebar_label: Basic Concepts
sidebar_position: 1
description: Learn about the Structured Query language (SQL), its basic concepts, data types, operators, and commands that form the foundation of database manipulation.
tags:
- DBMS
- SQL
- Database Design
---

# DBMS - SQL Basic Concepts

SQL stands for Structured Query Language. It is used to access and manipulate data in databases. By executing queries SQL can *create*, *update*, *delete*, and *retrieve* data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.

## Why SQL?
SQL helps to easily get information from data with high efficiency. Best Part? Without a lot of coding knowledge, we can manage a database with SQL. Anyone who knows English can master SQL queries in no time.
When we are executing the command of SQL on any Relational database managemnet system, then the system automatically finds the best routine to carry out our requests, and the SQL engine determines how to interpret the particular command.


## SQL DATABASE
The very first step is to store the information in database, hence, we will first create a database.

1. **CREATE:**
To create a new database in SQL we use this command. Note that blank spaces are not allowed in the name and is case-insenitive.
```sql
CREATE DATABASE database_name;
2. **SHOW:**
To view all the databases, we can use the keyword show. It returns a list of all the databases that exist in our system.
```sql
SHOW DATABASE;
3. **USE:**
To change the database or select another database, we use the command:
```sql
USE database_name;
4. **DROP:**
It is used to remove the entire database from the system. Once deleted, it can not be retrieved.
We can use the if exists clause to avoid any errors.
```sql
DROP DATABASE database_name;
DROP DATABASE IF EXISTS database_name;
5. **RENAME:**
It is used to rename the database.
```sql
RENAME DATABASE former_database_name TO new_database_name;

## SQL TABLES
Now we have created the database. We will create tables inside our database. They are very similar to spreadsheets, which store data in very organized grid format. We can create as many tables as we require.
1. **CREATE:**
To create a new table in database we use this command. We define the structure of table and the datatypes of columns.
```sql
CREATE table Employee(
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(50),
Salary DECIMAL(10, 2)
);
2. **DELETE:**
It is used to delete data in a database. We selectively remove records from a database table based on certain conditions.
```sql
DELETE FROM table_name WHERE some_condition;
3. **DROP:**
It is used to delete data and structure of the table from the database permanently.
```sql
DROP TABLE table_name;
4. **ALTER:**
It is used to rename the table.
```sql
ALTER TABLE former_table_name RENAME TO new_table_name;
203 changes: 203 additions & 0 deletions docs/DBMS/Structured Query Language/sql-clauses-operators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
---
id: sql-clauses-operators
title: DBMS - SQL Clauses & Operators
sidebar_label: Clauses & Operators
sidebar_position: 2
description: Learn about the SQL clauses and operators.
tags:
- DBMS
- SQL-Operators
- SQL
- Database Design
---

# DBMS - SQL Clauses & Operators

In SQL, clauses and operators play a crucial role in forming queries that manipulate and retrieve data from databases. Understanding these elements is essential for effective database management and query execution.

## SQL Clauses

SQL clauses are used to specify various conditions and constraints in SQL statements. Here are some of the most commonly used clauses:

1. **SELECT:**
The SELECT clause is used to retrieve data from a database.
```sql
SELECT column1, column2, ...
FROM table_name;
2. **WHERE:**
The WHERE clause is used to filter records based on a specified condition.
```sql
SELECT column1, column2, ...
FROM table_name
WHERE condition;
3. **ORDER BY:**
The ORDER BY clause is used to sort the result set in ascending or descending order.
```sql
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 ASC | DESC;
4. **GROUP BY:**
The GROUP BY clause is used to group rows that have the same values into summary rows.
```sql
SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1;
5. **HAVING:**
The HAVING clause is used to filter groups based on a specified condition, often used with GROUP BY.
```sql
SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1
HAVING condition;
6. **JOIN:**
The JOIN clause is used to combine rows from two or more tables based on a related column.
- **INNER JOIN:**
```sql
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
- **LEFT JOIN (or LEFT OUTER JOIN):**
```sql
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
- **RIGHT JOIN (or RIGHT OUTER JOIN):**
```sql
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
- **FULL JOIN (or FULL OUTER JOIN):**
```sql
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
```

## SQL Operators

SQL operators are used to perform operations on data. Here are some of the most commonly used operators:

1. **ARITHMETIC OPERATORS:**
Arithmetic operators are used to perform arithmetic operations on numeric data.

- **ADDITION:**
```sql
SELECT column1 + column2 AS result
FROM table_name;
```
- **SUBTRACTION:**
```sql
SELECT column1 - column2 AS result
FROM table_name;
```
- **MULTIPLICATION:**
```sql
SELECT column1 * column2 AS result
FROM table_name;
```
- **DIVISION:**
```sql
SELECT column1 / column2 AS result
FROM table_name;
```

2. **COMPARISON OPERATORS:**
Comparison operators are used to compare two values.

- **EQUAL TO:**
```sql
SELECT columns
FROM table_name
WHERE column = value;
```
- **NOT EQUAL TO:**
```sql
SELECT columns
FROM table_name
WHERE column <> value;
```
- **GREATER THAN:**
```sql
SELECT columns
FROM table_name
WHERE column > value;
```
- **LESS THAN:**
```sql
SELECT columns
FROM table_name
WHERE column < value;
```
- **GREATER THAN OR EQUAL TO:**
```sql
SELECT columns
FROM table_name
WHERE column >= value;
```
- **LESS THAN OR EQUAL TO:**
```sql
SELECT columns
FROM table_name
WHERE column <= value;
```

3. **LOGICAL OPERATORS:**
Logical operators are used to combine two or more conditions.

- **AND:**
```sql
SELECT columns
FROM table_name
WHERE condition1 AND condition2;
```
- **OR:**
```sql
SELECT columns
FROM table_name
WHERE condition1 OR condition2;
```
- **NOT:**
```sql
SELECT columns
FROM table_name
WHERE NOT condition;
```

4. **OTHER USEFUL OPERATORS:**

- **BETWEEN:** The BETWEEN operator selects values within a given range.
```sql
SELECT columns
FROM table_name
WHERE column BETWEEN value1 AND value2;
```
- **IN:** The IN operator allows you to specify multiple values in a WHERE clause.
```sql
SELECT columns
FROM table_name
WHERE column IN (value1, value2, ...);
```
- **LIKE:** The LIKE operator is used to search for a specified pattern in a column.
```sql
SELECT columns
FROM table_name
WHERE column LIKE pattern;
```
- **IS NULL:** The IS NULL operator is used to test for empty values (NULL).
```sql
SELECT columns
FROM table_name
WHERE column IS NULL;
```
- **IS NOT NULL:** The IS NOT NULL operator is used to test for non-empty values.
```sql
SELECT columns
FROM table_name
WHERE column IS NOT NULL;
```

This covers the basic SQL clauses and operators, which are essential for writing effective SQL queries. By mastering these elements, you can perform complex data manipulations and retrieve valuable insights from your database.
Loading