{"id":15002377,"url":"https://github.com/ialam085/a2z_sql_solutions","last_synced_at":"2026-03-16T13:09:17.030Z","repository":{"id":255745472,"uuid":"853243829","full_name":"ialam085/A2Z_SQL_Solutions","owner":"ialam085","description":"The objective of the SQL report is to understand all the Basic SQL-Server Queries/Scripts as well as all kind of String Functions","archived":false,"fork":false,"pushed_at":"2024-11-07T20:40:25.000Z","size":774,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-18T12:15:55.776Z","etag":null,"topics":["sql","sql-server"],"latest_commit_sha":null,"homepage":"https://github.com/ialam085/A2Z_SQL_Solutions","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ialam085.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-06T09:15:17.000Z","updated_at":"2024-11-07T20:40:28.000Z","dependencies_parsed_at":"2024-09-24T18:49:54.786Z","dependency_job_id":"9324aaad-85f5-4432-84e7-3ab616cec06c","html_url":"https://github.com/ialam085/A2Z_SQL_Solutions","commit_stats":{"total_commits":141,"total_committers":1,"mean_commits":141.0,"dds":0.0,"last_synced_commit":"00a3e1f61f68d1e20be244ce34a71e8878400f23"},"previous_names":["ialam085/sql_server_practice_all_queries","ialam085/a2z_sql_solutions"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ialam085%2FA2Z_SQL_Solutions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ialam085%2FA2Z_SQL_Solutions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ialam085%2FA2Z_SQL_Solutions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ialam085%2FA2Z_SQL_Solutions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ialam085","download_url":"https://codeload.github.com/ialam085/A2Z_SQL_Solutions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243146728,"owners_count":20243739,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["sql","sql-server"],"created_at":"2024-09-24T18:49:57.023Z","updated_at":"2025-12-24T13:53:34.919Z","avatar_url":"https://github.com/ialam085.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🔳 `A` ${\\color{Red}2}$ `Z` SQL Solutions ${\\color{Green}(using\\ SQL\\ SERVER)}$\n\n![image](https://github.com/user-attachments/assets/41e9ec22-6d13-45a3-94ed-75b2a6e7bc5e) \n## ©️ ${\\color{grey}Developed\\ and\\ Maintained\\ by:}$ ${\\color{blue}Md\\ Imtiyaz\\ Alam}$ \n\n\n## 👇 ${\\color{red}NAVIGATION}$\n\n| Categories | Sub_Categories | | | | |\n|------------|----------------|---|---|---|---|\n| [DDL](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-ddl-data-definition-language) ▶️| [SCHEMA](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorblueschema) | [CREATE](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluecreate) | [ALTER](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluealter) | [DROP](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluedrop) | [TRUNCATE](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluetruncate) |\n| [DML](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-dml-data-manipulation-language) ▶️| [INSERT](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorblueinsert) | [UPDATE](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorblueupdate) | [DELETE](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluedelete) | [STORED_PROCEDURE](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluestored-procedure) | |\n| [DQL](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-dql-data-query-language) ▶️| [SELECT](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorblueselec-t) | [WILDCARDS](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluewildcards) | [CLAUSES](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorblueclauses) | [JOINS](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluejoin_s) | |\n| [DCL](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-dcl-data-control-language) ▶️| [GRANT](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluegrant) | [REVOKE](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluerevoke) | | | |\n| [TCL](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-tcl-transaction-control-language) ▶️| [COMMIT](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluecommit) | [ROLLBACK](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluerollback) | [SAVEPOINT](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluesavepoint) | [TRANSACTION](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorblueset-transaction) | |\n| [SFL](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-sfl-scalar-functions-library) ▶️| [STRING_FUNC](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluestring-functions) | [MATH_FUNC](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluemathematical-functions) | [DATE_FUNC](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluedate-functions) | [LOGIC_FUNC](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorbluelogical-functions) | [CONVERT_FUNC](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorblueconversion-functions) |\n| [WFL](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-wfl-windows-functions-library) ▶️| [AGGREGATE_FUNC](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorblueaggregate-functions) | [RANK_FUNC](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorblueranking-functions) | [ANALY_FUNC](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorblueanalytic-functions) | | |\n| OTHERS ▶️| [Use](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-colorblueuse-scl) | [Constraints](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-create-the-table-student-with-constraints-inline) | [View](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-create-a-view-named-class10_students) | [Index](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-create-an-index-idx_studname) | [Normalization](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-create-tables-under-normalization) |\n|  | [Rename](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-rename-a-table-students-to-student) | [Set](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-update-set-single-valuerow-class-in-a-table-student) | [Data_Warehouse](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-insert-and-copy-complete-datavalues-from-old-source-table-student_old-into-a-new-destination-table-student-with-same-columns) | [Trigger](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-update-set-single-valuerow-class-in-a-table-student) | [CTE](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-display-the-n-th-rank-marks-obtained-using-dense_rank-with-cte) |\n|  | [Arith_Operator](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-select-records-with-a-arithmetic-operators------) | [Comp_Operator](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-select-records-with-a-condition-comparison-operators---------) | [Log_Operator](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-select-records-with-conditions-by-logical-operators-and-or-not-in-between-like-anysome-all-is-null) | [Union](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-fetch-student-records-using-union) | [Intersect](https://github.com/ialam085/A2Z_SQL_Solutions/blob/main/README.md#-fetch-student-records-using-intersect) |\n\n\n\n# ◻️ Objective\n\n- The objective of the SQL report is to understand all the **Basic SQL-Server Queries/Scripts** as well as all kind of **String Functions**.\n\n# ◻️ Tech Stack\n\n- SQL Server\n\n# ◻️ Steps includes\n\n- Creating a Database `FSA`\n- Creating two Tables `Student` and `Exams`\n- Applying all `categories` of SQL Commands\n     \n   - **${\\color{blue}DDL}$**: Defines Database structures.\n \n   - **${\\color{blue}DML}$**: Manipulates Data.\n \n   - **${\\color{blue}DQL}$**: Queries and Retrieves data.\n\n   - **${\\color{blue}DCL}$**: Manages access Permissions.\n\n   - **${\\color{blue}TCL}$**: Controls Transactions.\n \n   - **${\\color{blue}SFL}$**: Returns a single value.\n \n   - **${\\color{blue}WFL}$**: Returns a value based on a set of rows.\n\n# ◻️ Categories of applied SQL Commands\n\n- **${\\color{red}DDL}$** `(Data Definition Language)`: _It changes the structure of the table like creating a table, deleting a table, altering a table, etc. All the command of DDL are auto-committed that means it permanently save all the changes in the database._\n\n   - SCHEMA\n   - CREATE `[Define DATA TYPES, CONSTRAINTS here]`\n   - ALTER `(Rename/Add/Drop)`\n   - DROP\n   - TRUNCATE\n\n- **${\\color{red}DML}$** `(Data Manipulation Language)`: _DML commands are used to modify the database. The command of DML is not auto-committed that means it can't permanently save all the changes in the database. They can be rollback._\n\n   - INSERT\n   - UPDATE\n   - DELETE\n   - STORED PROCEDURE\n\n- **${\\color{red}DQL}$** `(Data Query Language)`: _DQL is used to fetch the data from the database._\n\n   - SELECT `[mostly OPERATORS used here]`\n   - CLAUSES `[WHERE, GROUP BY, HAVING, ORDER BY, LIMIT/TOP]`\n   - JOINS `[JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, SELF JOIN, CROSS JOIN, UNION, UNION ALL, INTERSECT]`\n\n- **${\\color{red}DCL}$** `(Data Control Language)`: _DCL commands are used to Grant and Revoke (take back) authority from any database user._\n\n   - GRANT\n   - REVOKE\n\n- **${\\color{red}TCL}$** `(Transaction Control Language)`: _TCL commands can only use with DML commands like INSERT, DELETE and UPDATE only._\n\n   - BEGIN TRANSACTION\n   - COMMIT\n   - ROLLBACK\n   - SAVEPOINT\n\n- **${\\color{red}SFL}$** `(Scalar Functions Library)`: _SFL commands are used to return a single value based on the input provided._\n\n   - STRING FUNCTIONS `[CONCAT, SUBSTRING, CHARINDEX, PATINDEX, CHAR LENGTH, LETTER CASE, TRIM, REPLACE, LEFT/RIGHT, REVERSE, REPLICATE, FORMAT]`\n   - MATHEMATICAL FUNCTIONS `[ABS, ROUND, SQRT, CEILING, FLOOR, POWER, EXP, LOG]`\n   - DATE FUNCTIONS `[GETDATE, YEAR, MONTH, DAY, DATEADD, DATEDIFF]`\n   - CONVERSION FUNCTIONS `[CAST, CONVERT, TRY_CAST, TRY_CONVERT]`\n   - LOGICAL FUNCTIONS `[COALESCE, ISNULL]`\n\n- **${\\color{red}WFL}$** `(Windows Functions Library)`: _WFL commands are used to return a value based on a set of rows related to the current row._\n\n   - AGGREGATE FUNCTIONS `[COUNT, AVG, SUM, MIN, MAX]`\n   - RANKING FUNCTIONS `[ROW_NUMBER, DENSE_RANK, RANK, NTILE]`\n   - ANALYTIC FUNCTIONS `[CUME_DIST, PERCENT_RANK, FIRST_VALUE, LAST_VALUE, LAG, LEAD]`\n\n\n\n# ◻️ Detailed view of all *SQL-Server* Commands with `QUERIES` and `SCRIPTS`\n\n```sql\nTable: STUDENT\n+-------------+-----------+-----------------+--------+-----------------+---------+----------------+-------+-----+------------+\n|   Adm_No    | DOJ       | Stud_Name       | Gender | Guardian_Name   | Address | Contact_Number | Class | Fee | Monitor_ID |\n+-------------+-----------+-----------------+--------+-----------------+---------+----------------+-------+-----+------------+\n|  ROSE00023  | 10/1/2021 | Abu Talha       |   M    | Md Fareed       | Delhi   |   7903077297   |   10  | 400 | NULL       |\n|  ROSE00024  | 10/1/2021 | Abu Salesh      |   M    | Md Fareed       | Delhi   |   7903077297   |   8   | 450 | ROSE00023  |\n|  ROSE00040  | 10/1/2021 | Md Neyamul      |   M    | Md Shamsuddin   | Chennai |   9661194838   |   7   | 350 | ROSE00023  |\n|  ROSE00041  | 6/8/2021  | Ruba Parveen    |   F    | Md Parwez       | Delhi   |   9693461570   |   5   | 275 | ROSE00023  |\n|  ROSE00058  | 10/2/2021 | Md Muntazeem    |   M    | Md Naimuddin    | Pune    |   8292149189   |   10  | 325 | ROSE00023  |\n|  ROSE00102  | 10/29/2021| Mantasha Khatoon|   F    | Hasnain         | Noida   |   9709148101   |   6   | 250 | ROSE00144  |\n|  ROSE00144  | 12/1/2021 | Arju Kumar      |   M    | Ranjit Kumar Sah| Mumbai  |   6206863026   |   8   | 300 | ROSE00058  |\n|  ROSE00145  | 12/1/2021 | Roji Kumari     |   F    | Ranjit Kumar Sah| Mumbai  |   6206863026   |   7   | 300 | ROSE00144  |\n|  ROSE00172  | 12/4/2021 | Md Azfar        |   M    | Md Mushtaque    | Sikkim  |   7631041561   |   10  | 300 | ROSE00058  |\n|  ROSE00331  | 2/3/2023  | Juveria Khatoon |   F    | Saud Alam       | Chennai |   7330859950   |   8   | 300 | ROSE00058  |\n|  ROSE00335  | 9/4/2023  | Manish Kumar    |   M    | Ranjan Kumar    | Noida   |   9060609777   |   9   | 350 | ROSE00058  |\n|  ROSE00041  | 6/8/2021  | Ruba Parveen    |   F    | Md Parwez       | Delhi   |   9693461570   |   5   | 275 | ROSE00144  |\n+-------------+-----------+-----------------+--------+-----------------+---------+----------------+-------+-----+------------+\n```\n```sql\nTable: EXAMS\n+------------+--------------+--------------+----------------+------------+\n|   Adm_No   | Subject_Code | Subject_Name | Marks_Obtained | Exam_Date  |\n+------------+--------------+--------------+----------------+------------+\n|  ROSE00001 |    SUB004    | S.St.        |      73        | 12/11/2021 |\n|  ROSE00015 |    SUB002    | Science      |      66        | 12/13/2021 |\n|  ROSE00023 |    SUB003    | English      |      95        | 12/09/2021 |\n|  ROSE00024 |    SUB001    | Mathematics  |      86        | 12/10/2021 |\n|  ROSE00040 |    SUB002    | Science      |      80        | 12/08/2021 |\n|  ROSE00041 |    SUB002    | Science      |      78        | 12/02/2021 |\n|  ROSE00050 |    SUB003    | English      |      56        | 12/14/2021 |\n|  ROSE00051 |    SUB001    | Mathematics  |      67        | 12/15/2021 |\n|  ROSE00058 |    SUB001    | Mathematics  |      90        | 12/07/2021 |\n|  ROSE00065 |    SUB002    | Science      |      81        | 12/16/2021 |\n|  ROSE00102 |    SUB001    | Mathematics  |      85        | 12/01/2021 |\n|  ROSE00108 |    SUB005    | Computer     |         NULL   |   NULL     |\n|  ROSE00144 |    SUB002    | Science      |      74        | 12/05/2021 |\n|  ROSE00145 |    SUB003    | English      |      89        | 12/06/2021 |\n|  ROSE00172 |    SUB003    | English      |      92        | 12/03/2021 |\n|  ROSE00331 |    SUB001    | Mathematics  |      88        | 12/04/2021 |\n+------------+--------------+--------------+----------------+------------+\n```\n\n# 📗 DDL (`Data Definition Language`)\n\n\n# 🔘 ${\\color{blue}USE}$ (SCL)\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+---------------------------------------------------------------------------------------------------------------------------------+\n| Specifically it comes under SCL (Session Control Language). It is used to select a specific Database to work with in a session. |\n+---------------------------------------------------------------------------------------------------------------------------------+\n```\n### 🔸 Using an Existing `DATABASE` named 'FSA'\n```sql      \n      USE FSA;\n```\n\n# 🔘 ${\\color{blue}SCHEMA}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+----------------------------------------------------------------------------------------------------------------------------------+\n| A schema is like a container for Database objects (tables) that can contain multiple relational tables for a project/department. |\n| A database might have multiple schemas                                                                                           |\n+----------------------------------------------------------------------------------------------------------------------------------+\n```\n### 🔹 Create a new `SCHEMA` named 'INSTITUTE'\n```sql      \n      CREATE SCHEMA INSTITUTE;\n```\n\n### 🔹 Create a Table `within Schema` INSTITUTE\n```sql      \n      CREATE TABLE INSTITUTE.RESULT (\n                                      Adm_No VARCHAR(20) PRIMARY KEY,\n                                      Tot_Marks INT,\n                                      Grade VARCHAR(30)\n                                    );\n```\n\n### 🔹 Transfer Table `Student` into Schema `INSTITUTE`\n```sql      \n      ALTER SCHEMA INSTITUTE TRANSFER dbo.Student;     -- dbo.student means student table in current database (dbo) transferring to schema 'Institute'\n```\n```sql      \n      ALTER SCHEMA INSTITUTE TRANSFER HR.Student;     -- HR.student means student table in Old schema (HR) transferring to New schema 'Institute' in current database\n```\n\n\n# 🔘 ${\\color{blue}CREATE}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+--------------------------------------------------------------------------+\n| It is used to Create new Databases, Tables, Constraints, Views, Indexes. |\n+--------------------------------------------------------------------------+\n```\n### 🔸 Create a new `DATABASE` named 'FSA'\n```sql\n      CREATE DATABASE FSA;\n```\n### 🔸 Create a new `TABLE` named 'STUDENT'\n```sql\n      CREATE TABLE STUDENT (\n      Adm_No VARCHAR(20) PRIMARY KEY,\n      DOJ DATE,\n      Stud_Name VARCHAR(50),\n      Gender CHAR(10),\n      Guardian_Name VARCHAR(50),\n      Address VARCHAR(150),\n      Contact_Number BIGINT,\n      Class INT,\n      Fee DECIMAL(10, 2)\n      );\n```\n### 🔸 Create the Table Student with `CONSTRAINTS` (inline)\n- **SQL `CONSTRAINTS` are used to specify `rules` for the data in a table. Constraints are used to `limit` the type of data that can go into a table.**\n```sql\n      CREATE TABLE STUDENT (\n      Adm_No VARCHAR(10) PRIMARY KEY,                    -- PRIMARY KEY constraint (takes by default NOT NULL) constraint on Admission number\n      DOJ DATE NOT NULL,                                 -- Date of Joining, NOT NULL constraint\n      Stud_Name VARCHAR(50) NOT NULL,                    -- Student Name, NOT NULL constraint\n      Gender CHAR(1) CHECK (Gender IN ('M', 'F')),       -- CHECK constraint ensuring Gender is either 'M' or 'F'\n      Guardian_Name VARCHAR(50) NOT NULL,                -- Guardian Name, NOT NULL constraint\n      Address VARCHAR(100),                              -- Address\n      Contact_Number VARCHAR(15),                        -- Contact Number\n      Class INT CHECK (Class BETWEEN 1 AND 12),          -- CHECK constraint ensuring Class is between 1 and 12\n      Fee DECIMAL(10, 2) CHECK (Fee \u003e 0)                 -- CHECK constraint ensuring Fee is positive\n      Class_Time time(10) DEFAULT '07:05:00' NOT NULL    -- DEFAULT with NOT NULL, 2 constraint, IF USER DOESN'T INSERT ANY VALUE IN Class_Time, WILL AUTOMATICALLY TAKE Default Time '07:05:00'\n      );\n```\n### 🔸 Create a `VIEW` named `Class10_Students`\n- **SQL `VIEWS` are simplified data access, minimize the Query. It is also known as `Virtual Table` or `Query Table` because it does not store the rows and columns on the disk. It can lead to performance issues because it is not actual table**\n```sql\n      CREATE VIEW Class10_Students AS\n      SELECT Adm_No, Stud_Name, Gender, Guardian_Name, Contact_Number, Fee\n      FROM STUDENT\n      WHERE Class = 10;\n```\n- **Query to check the `VIEWS` in a Table**\n```sql\n      SELECT * FROM Class10_Students;\n```\n### 🔸 Create an `INDEX` `idx_StudName`\n- **An Index in SQL is like a table of contents in a book. It helps SQL Server quickly locate and retrieve the data from a table without having to scan the entire table.**\n```sql\n      CREATE INDEX idx_StudName\n      ON STUDENT (Stud_Name);\n```\n- **Query to check the `INDEXES` in a Table**\n```sql\n      EXEC sp_helpindex 'STUDENT';\n```\n![image](https://github.com/user-attachments/assets/7b6a57fc-0b20-4b4e-b912-d84ee18f6013)\n\n- **Without an Index (_Left side_)**: SQL Server `searches the whole table` (**slow**).\n- **With an Index (_Right side_)**: SQL Server `jumps directly to the rows` you're looking for (**fast**).\n\n### 🔸 Create Tables under `NORMALIZATION`\n- **`NORMALIZATION`: The process of organizing data to reduce complexity and improve data integrity.**\n\n- **`1st Normal Form (1NF)`**: Eliminate repeating groups.\n- **`Before 1NF`**\n```sql\n      \t\t\t\t\t-- Original table with multiple phone numbers in one row\n\tCREATE TABLE StudentInfo (\n\t\t\t\t   Adm_No VARCHAR(10),\n    \t\t\t\t   Stud_Name VARCHAR(50),\n    \t\t\t\t   Phone_Number VARCHAR(100)      -- Comma-separated values\n\t\t\t\t );\n\n\t\t\t\t\t-- Inserting a record with multiple phone numbers\n\tINSERT INTO StudentInfo (Adm_No, Stud_Name, Phone_Number)\n\t                 VALUES ('ROSE00023', 'Abu Talha', '7903077297, 9823099999');\n```\n- **`After 1NF`**\n```sql\n      \t\t\t\t\t-- Creating a normalized table\n\tCREATE TABLE StudentInfo_1NF (\n                                        Adm_No VARCHAR(10),\n    \t\t\t\t\tStud_Name VARCHAR(50),\n    \t\t\t\t\tPhone_Number VARCHAR(15)\n\t\t\t\t     );\n\n\t\t\t\t\t-- Insert each phone number as a separate row\n\tINSERT INTO StudentInfo_1NF (Adm_No, Stud_Name, Phone_Number)\n\tVALUES ('ROSE00023', 'Abu Talha', '7903077297'),\n\t       ('ROSE00023', 'Abu Talha', '9823099999');\n```\n- **`2nd Normal Form (2NF)`**: Remove partial dependencies (Non-prime attributes depend on the whole primary key).\n- **`Before 2NF`**\n```sql\n\t\t\t\t-- Assume a student’s address and class information depend on both the Adm_No and the Class, which violates 2NF\n      \t\t\t\t\t\t-- Original table with partial dependency\n\tCREATE TABLE StudentInfo_2NF (\n    \t\t\t\t\tAdm_No VARCHAR(10),\n    \t\t\t\t\tClass VARCHAR(10),\n    \t\t\t\t\tStud_Name VARCHAR(50),\n    \t\t\t\t\tAddress VARCHAR(50)\n\t\t\t\t     );\n\n\t\t\t\t\t\t-- Inserting data\n\tINSERT INTO StudentInfo_2NF (Adm_No, Class, Stud_Name, Address)\n\t                     VALUES ('ROSE00023', '10', 'Abu Talha', 'Delhi');\n```\n- **`After 2NF`**\n```sql\n\t\t\t-- Create separate tables for Student and Class\n      \t\t\t\t\t\t-- Student table (Adm_No is the primary key)\n\tCREATE TABLE Student (\n    \t\t\t\tAdm_No VARCHAR(10),\n    \t\t\t\tStud_Name VARCHAR(50),\n    \t\t\t\tAddress VARCHAR(50)\n\t\t\t     );\n\n\t\t\t\t\t\t-- Class table (Separate the class info)\n\tCREATE TABLE ClassInfo (\n    \t\t\t\tAdm_No VARCHAR(10),\n    \t\t\t\tClass VARCHAR(10)\n\t\t\t       );\n\n\t\t\t\t\t\t-- Insert into the Student table\n\tINSERT INTO Student (Adm_No, Stud_Name, Address)\n\t             VALUES ('ROSE00023', 'Abu Talha', 'Delhi');\n\n\t\t\t\t\t\t-- Insert into the ClassInfo table\n\tINSERT INTO ClassInfo (Adm_No, Class)\n\t               VALUES ('ROSE00023', '10');\n```\n- **`3rd Normal Form (3NF)`**: Remove transitive dependencies (Non-prime attributes depend on non-prime attributes).\n- **`Before 3NF`**\n```sql\n\t\t-- Assume the Guardian_Name and Contact_Number depend on the student’s Adm_No, but the Contact_Number is dependent on the Guardian_Name, violating 3NF\n      \t\t\t\t\t\t-- Original table with transitive dependency\n\tCREATE TABLE StudentGuardianInfo (\n\t\t\t\t\t   Adm_No VARCHAR(10),\n    \t\t\t\t\t   Stud_Name VARCHAR(50),\n    \t\t\t\t\t   Guardian_Name VARCHAR(50),\n    \t\t\t\t\t   Contact_Number VARCHAR(15)\n\t\t\t\t\t );\n\n\t\t\t\t\t\t-- Inserting data\n\tINSERT INTO StudentGuardianInfo (Adm_No, Stud_Name, Guardian_Name, Contact_Number)\n\t                         VALUES ('ROSE00023', 'Abu Talha', 'Md Fareed', '7903077297');\n```\n- **`After 3NF`**\n```sql\n\t\t\t-- Create a separate table for Guardian to remove the transitive dependency\n      \t\t\t\t\t-- Student table (Adm_No is the primary key)\n\tCREATE TABLE Student (\n    \t\t\t\tAdm_No VARCHAR(10),\n    \t\t\t\tStud_Name VARCHAR(50)\n\t\t\t     );\n\n\t\t\t\t\t-- Guardian table (Guardian_Name is the primary key)\n\tCREATE TABLE Guardian (\n    \t\t\t\tGuardian_Name VARCHAR(50),\n    \t\t\t\tContact_Number VARCHAR(15)\n\t\t\t      );\n\n\t\t\t\t\t-- StudentGuardianInfo table to link students to their guardians\n\tCREATE TABLE StudentGuardianInfo (\n\t\t\t\t\t   Adm_No VARCHAR(10),\n    \t\t\t\t\t   Guardian_Name VARCHAR(50)\n\t\t\t\t\t );\n\n\t\t\t\t\t-- Insert into the Student table\n\tINSERT INTO Student (Adm_No, Stud_Name)\n\t             VALUES ('ROSE00023', 'Abu Talha');\n\n\t\t\t\t\t-- Insert into the Guardian table\n\tINSERT INTO Guardian (Guardian_Name, Contact_Number)\n\t              VALUES ('Md Fareed', '7903077297');\n\n\t\t\t\t\t-- Insert into the StudentGuardianInfo table\n\tINSERT INTO StudentGuardianInfo (Adm_No, Guardian_Name)\n\t                         VALUES ('ROSE00023', 'Md Fareed');\n```\n\n# 🔘 ${\\color{blue}ALTER}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+---------------------------------------------------------------------------------------+\n| It is used to Alter (change) the structure of the Table and the name of the Database. |\n+---------------------------------------------------------------------------------------+\n```\n### 🔹 Alter a `DATABASE` _FSA_ to 'FSA_new'\n```sql \n      ALTER DATABASE FSA\n      Modify Name = FSA_new;\n```\n### 🔹 Rename a `Table` _STUDENTS_ to 'STUDENT'\n```sql      \n      EXEC sp_rename 'Students', 'Student';\n```\n### 🔹 Rename a Table `Column` _Contact_No_ to 'Contact_Number'\n```sql      \n      EXEC sp_rename 'student.Contact_No', 'Contact_Number';\n```\n### 🔹 Add a new `column` 'Email' to table _Student_\n```sql      \n      ALTER TABLE STUDENT\n      ADD Email VARCHAR(100);\n```\n### 🔹 Modify a `column (change data type)` _BIGINT_ to 'VARCHAR' for `Contact_Number` column\n```sql      \n      ALTER TABLE STUDENT\n      ALTER COLUMN Contact_Number VARCHAR(20);\n```\n### 🔹 Modify a `column (change length of data type)` _VARCHAR(50)_ to 'VARCHAR(100)' for `Stud_Name` column\n```sql      \n      ALTER TABLE STUDENT\n      ALTER COLUMN Stud_Name VARCHAR(100);\n```\n### 🔹 Drop a column 'Email' from table _Student_\n```sql      \n      ALTER TABLE STUDENT\n      DROP COLUMN Email;\n```\n### 🔹 Add a default value of `300` to 'Fee' column by adding CONSTRAINT\n```sql      \n      ALTER TABLE STUDENT\n      ADD CONSTRAINT DF_Fee DEFAULT 300 FOR Fee;\n```\n\n\n# 🔘 ${\\color{blue}DROP}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+-----------------------------------------------------------------------+\n| It is used to Delete/Remove the objects from the Database completely. |\n+-----------------------------------------------------------------------+\n```\n### 🔸 Drop the `Database` 'FSA'\n```sql      \n      DROP DATABASE FSA;\n```\n### 🔸 Drop the `Schema` 'HR'\n```sql      \n      DROP SCHEMA HR;\n```\n### 🔸 Drop the `Table` 'Student'\n```sql      \n      DROP TABLE STUDENT;\n```\n### 🔸 Drop a column 'Email' from table _Student_\n```sql      \n      ALTER TABLE STUDENT\n      DROP COLUMN Email;\n```\n### 🔸 Drop the `View` 'Class10_Students'\n```sql      \n      DROP VIEW Class10_Students;\n```\n### 🔸 Drop an `Index` 'idx_StudName'\n```sql      \n      DROP INDEX idx_StudName ON STUDENT;          -- Syntax: Index_Name ON Table_Name\n```\n```sql      \n      Drop Index Student.idx_StudName;             -- Syntax: Table_Name.Index_Name\n```\n\n\n# 🔘 ${\\color{blue}TRUNCATE}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+-----------------------------------------------------------------------------------------------------------------------+\n| It is used to Remove/Delete all records (rows) from a table, but the table structure (Column names/headings) remains. |\n+-----------------------------------------------------------------------------------------------------------------------+\n```\n### 🔹 Truncate the `Table` 'Student'\n```sql      \n      TRUNCATE TABLE STUDENT;\n```\n\n\n# 📗 DML (`Data Manipulation Language`)\n\n\n# 🔘 ${\\color{blue}INSERT}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+-------------------------------------------------+\n| It is used to Add new data/values into a table. |\n+-------------------------------------------------+\n```\n- **'Column names' `must match the order` of the values.**\n- **'Dates' should be provided in `YYYY-MM-DD` format.**\n- **The 'Adm_No' is the `primary key` and must be `unique` for each row.**\n  \n### 🔹 Insert Data/Values (_single record_) into a Table `Student`\n```sql      \n      INSERT INTO STUDENT (Adm_No, DOJ, Stud_Name, Gender, Guardian_Name, Address, Contact_Number, Class, Fee)\n      VALUES ('ROSE00023', '2021-10-01', 'Abu Talha', 'M', 'Md Fareed', 'Khiripaghar', '7903077297', 10, 400);\n```\n### 🔹 Insert Data/Values (_Multiple records_) into a Table `Student`\n```sql      \n      INSERT INTO STUDENT (Adm_No, DOJ, Stud_Name, Gender, Guardian_Name, Address, Contact_Number, Class, Fee)\n      VALUES \n      ('ROSE00023', '2021-10-01', 'Abu Talha', 'M', 'Md Fareed', 'Khiripaghar', '7903077297', 10, 400),\n      ('ROSE00024', '2021-10-01', 'Abu Salesh', 'M', 'Md Fareed', 'Khiripaghar', '7903077297', 8, 450),\n      ('ROSE00040', '2021-10-01', 'Md Neyamul', 'M', 'Md Shamsuddin', 'Gauripur', '9661194838', 7, 350),\n      ('ROSE00041', '2021-06-08', 'Ruba Parveen', 'F', 'Md Parwez', 'Khiripaghar', '9693461570', 5, 275),\n      ('ROSE00058', '2021-10-02', 'Md Muntazeem', 'M', 'Md Naimuddin', 'Rajapur', '8292149189', 10, 325),\n      ('ROSE00102', '2021-10-29', 'Mantasha Khatoon', 'F', 'Hasnain', 'Nayadih', '9709148101', 6, 250),\n      ('ROSE00144', '2021-12-01', 'Arju Kumar', 'M', 'Ranjit Kumar Sah', 'Chilmil', '6206863026', 8, 300),\n      ('ROSE00145', '2021-12-01', 'Roji Kumari', 'F', 'Ranjit Kumar Sah', 'Chilmil', '6206863026', 7, 300),\n      ('ROSE00172', '2021-12-04', 'Md Azfar', 'M', 'Md Mushtaque', 'Maghota', '7631041561', 10, 300),\n      ('ROSE00331', '2023-02-03', 'Juveria Khatoon', 'F', 'Saud Alam', 'Chihar', '7330859950', 8, 300);\n```\n### 🔹 Insert and COPY complete Data/Values from Old Source Table `Student_Old` into a New Destination Table `Student` with `Same Columns`\n- **`Data Warehousing`: It is the process of storing and managing large volumes of data from multiple sources for analysis and reporting.**\n- **`ETL`: The process of combining data from multiple sources into a data warehouse is called extract, transform, and load (ETL).**\n- **`Data Mining`: Discovering patterns, trends, and useful information from large datasets stored in a database like `Aggregation`, or other `calculations`.**\n```sql      \n      INSERT INTO STUDENT (Adm_No, DOJ, Stud_Name, Gender, Guardian_Name, Address, Contact_Number, Class, Fee)\n\t\t   SELECT Adm_No, DOJ, Stud_Name, Gender, Guardian_Name, Address, Contact_Number, Class, Fee\n      FROM Student_Old;\n```\n\n# 🔘 ${\\color{blue}UPDATE}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+-----------------------------------------------------------+\n| It is used to Modify existing data/values within a table. |\n+-----------------------------------------------------------+\n```\n- **The `WHERE` clause ensures that only the specified row (e.g., Adm_No = 'ROSE00023') is updated.**\n- **Without `WHERE`, all rows in the table would be updated!**\n  \n### 🔸 Update (SET) `Single value/row` (_Class_) in a Table 'Student'\n```sql     \n     UPDATE STUDENT\n     SET Class = 9\n     WHERE Adm_No = 'ROSE00040';\n```\n### 🔸 Update (SET) `Multiple (same) values/same Column` (_Gender_) in a Table 'Student' if Column has NULL values\n```sql     \n     UPDATE STUDENT\n     SET Gender = 'M'\n     Where Gender IS NULL;\n```\n### 🔸 Update (SET) `Multiple (different) values/same row` (_Stud_Name, Fee and DOJ_) in a Table 'Student'\n```sql     \n     UPDATE STUDENT\n     SET Stud_Name = 'Abu Talha Khan', Fee = 450, DOJ = '2021-10-04'\n     WHERE Adm_No = 'ROSE00023';\n```\n### 🔸 Update (SET) `Multiple (same) values/same Column` (_Gender_) in a Table 'Student'\n```sql     \n     UPDATE STUDENT\n     SET Gender = 'Fem'\n     WHERE Adm_No IN ('ROSE00145', 'ROSE00331', 'ROSE00041');\n```\n### 🔸 Update (SET with CASE, ELSE, END) `Multiple (different) values/different Column` (_FEE_) in a Table 'Student'\n- **for `ELSE` statement when we keep the same column name (i.e. \"Fee\") as `SET` statement, then Rest columns `value remain same`**\n```sql\n        UPDATE STUDENT\n           SET Fee = CASE\n               WHEN Adm_No='ROSE00024' THEN 450\n               WHEN Adm_No='ROSE00041' THEN 275\n               WHEN Adm_No='ROSE00058' THEN 325\n               WHEN Adm_No='ROSE00102' THEN 250\n               WHEN Adm_No='ROSE00023' THEN 400\n               WHEN Adm_No='ROSE00040' THEN 350\n           ELSE Fee                                       -- for ELSE statement when we keep the same column name (i.e. \"Fee\") as SET statement, then Rest columns value remain same\n        END;\n```\n### 🔸 Update (SET with CASE, ELSE, END) `Multiple (different) values/different Column` (_FEE_) in a Table 'Student'\n- **for `ELSE` statement when we keep any value (i.e. \"199\") different from `SET` statement, then Rest columns take the `default value \"199\"`**\n```sql\n        UPDATE STUDENT\n           SET Fee = CASE\n               WHEN Adm_No='ROSE00024' THEN 450\n               WHEN Adm_No='ROSE00041' THEN 275\n               WHEN Adm_No='ROSE00058' THEN 325\n               WHEN Adm_No='ROSE00102' THEN 250\n               WHEN Adm_No='ROSE00023' THEN 400\n               WHEN Adm_No='ROSE00040' THEN 350\n           ELSE 199                                       -- for ELSE statement when we keep any value (i.e. \"199\") different from SET statement, then Rest columns take the default value \"199\"\n        END;\n```\n\n\n# 🔘 ${\\color{blue}DELETE}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+-----------------------------------------+\n| It is used to Remove data from a table. |\n+-----------------------------------------+\n```\n- **WHERE clause specifies which rows to delete (e.g., rows with Adm_No = 'ROSE00023').**\n- **If you omit the WHERE clause, all rows in the table will be deleted!**\n\n### 🔹 Delete single Row/Record from a Table `Student` by the reference of one `Primary-Key` Value\n```sql      \n      DELETE FROM STUDENT\n      WHERE Adm_No = 'ROSE00023';                         -- One Primary-Key Value\n```\n### 🔹 Delete Multiple Rows/Records from a Table `Student` by the reference of many `Primary-Key` Values\n- **The `IN` operator allows you to match multiple values in a column.**\n- **This query will delete `Both rows` where Adm_No is either 'ROSE00023' or 'ROSE00024'.**\n```sql  \n      DELETE FROM STUDENT\n      WHERE Adm_No IN ('ROSE00023', 'ROSE00024');         -- Multiple Primary-Key Values by using \"IN\" operator\n```\n### 🔹 Delete Multiple Rows/Records from a Table `Student` by the reference of one `Non-Key` Value\n```sql      \n      DELETE FROM STUDENT\n      WHERE Class = 10;                                   -- One Non-Key Value\n```\n\n\n# 🔘 ${\\color{blue}STORED\\ PROCEDURE}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+---------------------------------------------------------------------------------------------+\n| It is a precompiled SQL code stored in the database, that can be executed as a single unit. |\n| If any SQL query need to write again and again then same can be saved as Stored Procedure.  |\n+---------------------------------------------------------------------------------------------+\n```\n\n### 🔹 CREATE a Table 'EmpInfo' `Outside Stored Procedure`\n```sql \n      CREATE TABLE EmpInfo (\n                      Emp_ID INT PRIMARY KEY,                    -- Constraint PRIMARY KEY (by default contains NOT NULL)\n                      Emp_Name VARCHAR(50) NOT NULL,             -- Constraint NOT NULL\n                      Department VARCHAR(50),\n                      Salary INT NOT NULL CHECK (Salary \u003e 10000) -- Constraints NOT NULL \u0026 Checks Salary more than 10,000\n                     );\n```\n### 🔹 `Stored Procedure` to `INSERT` Records into 'EmpInfo' Table\n```sql \n      CREATE PROCEDURE InsertEmpInfo\n\t\t\t\t     @Emp_ID INT,               -- '@' symbol is used to declare variables or parameters in SQL Server\n                                     @Emp_Name VARCHAR(50),\n                                     @Department VARCHAR(50),\n                                     @Salary INT\n      AS\n      BEGIN\n\t INSERT INTO EmpInfo (Emp_ID, Emp_Name, Department, Salary)\n\t              VALUES (@Emp_ID, @Emp_Name, @Department, @Salary);\n      END;\n```\n```sql\n                    -- SP can be called by EXEC command and pass the required parameters\n                    -- Inserting a new employee into the EmpInfo table, Can be inserted one or multi values/informations using EXEC command\n\n      EXEC InsertEmpInfo @Emp_ID = 1, @Emp_Name = 'John Doe', @Department = 'HR', @Salary = 10500;\n\n      EXEC InsertEmpInfo @Emp_ID = 2, @Emp_Name = 'Jane Smith', @Department = 'Finance', @Salary = 12000;\n```\n\n### 🔹 `Stored Procedure` to `UPDATE` Records into 'EmpInfo' Table\n```sql \n      CREATE PROCEDURE UpdateEmpInfo\n\t\t\t\t     @Emp_ID INT,\n                                     @Emp_Name VARCHAR(50),\n                                     @Department VARCHAR(50),\n                                     @Salary INT\n      AS\n      BEGIN\n\t UPDATE EmpInfo\n                     SET Emp_Name    =  @Emp_Name, \n                         Department  =  @Department, \n                         Salary      =  @Salary\n         WHERE Emp_ID = @Emp_ID;\n      END;\n```\n```sql\n      EXEC UpdateEmpInfo                                  -- SP can be called by EXEC command and pass the required parameters\n                         @Emp_ID = 1, \n                         @Emp_Name = 'Alice Brown', \n                         @Department = 'IT', \n                         @Salary = 12000;\n```\n### 🔹 `Stored Procedure` to `DELETE` Records from 'EmpInfo' Table\n```sql \n      CREATE PROCEDURE DeleteEmpInfo\n                                    @Emp_ID INT           -- Parameter to identify the employee\n      AS\n      BEGIN\n         DELETE FROM EmpInfo\n         WHERE Emp_ID = @Emp_ID;\n     END;\n```\n```sql\n      EXEC DeleteEmpInfo @Emp_ID = 1;                     -- Delete the employee with Emp_ID = 1\n```\n\n### 🔹 `Stored Procedure` to `SELECT` Records from 'EmpInfo' Table\n```sql      \n      CREATE PROCEDURE EmployeeInfo\n      AS                                  -- BEGIN and END are used to define the start and end of the executable block within a stored procedure,\n      BEGIN                               -- allowing multiple SQL statements to be grouped together. \n           SELECT * FROM EmpInfo\n           WHERE Department = 'HR';      \n      END;\n```\n### 🔹 Check Stored Procedure named `EmployeeInfo` from 'EmpInfo' Table\n```sql      \n      EXEC EmployeeInfo;\n```\n### 🔹 `Stored Procedure` using a `TRIGGER`\n- **`TRIGGER`: It is a special kind of stored procedure that automatically executes (or \"fires\") in response to specific events on a table, such as INSERT, UPDATE, or DELETE operations which helps in automate the tasks.**\n```sql      \n        CREATE TRIGGER trg_AfterInsert\n\tAFTER INSERT ON Employee\n\tFOR EACH ROW\n\tBEGIN\n    \t\tINSERT INTO Employee_Audit (Emp_ID, Change_Type, Change_Date)\n    \t\t                    VALUES (NEW.Emp_ID, 'INSERT', NOW());\n\tEND;\n```\n\n\n# 📗 DQL (`Data Query Language`)\n\n\n# 🔘 ${\\color{blue}SELEC T}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+------------------------------------------------------------+\n| It is used to Retrieve/Fetch data from one or more tables. |\n+------------------------------------------------------------+\n```\n  \n### 🔸 Select Current `Date and Time`\n```sql\n      SELECT Getdate() AS [Current Date and Time];             -- To check Current DATE and TIME\n```\n```sql\n      SELECT Sysdatetime() AS [Current Date and Time];         -- To check Current DATE and TIME\n```\n```sql      \n      SELECT Cast(Getdate() AS Date) AS [Current Date];        -- To check Current DATE only\n```\n```sql      \n      SELECT Cast(Sysdatetime() AS Date) AS [Current Date];    -- To check Current DATE only\n```\n### 🔸 Select `All Databases` of SQL-Server\n```sql      \n      SELECT * FROM sys.databases;\n```\n### 🔸 Copy a Table from Old Database to New Database\n```sql      \n      USE NewDB;\n      SELECT * INTO NewDB.dbo.Student FROM OldDB.dbo.Student;\n```\n### 🔸 Select `All Tables` from a current session Database\n```sql      \n      SELECT * FROM sys.tables;\n```\n### 🔸 Select `All Columns` from a Table 'Student'\n```sql      \n      SELECT * FROM STUDENT;\n```\n### 🔸 Select `All Columns` from a Table 'Exams'\n```sql      \n      SELECT * FROM EXAMS;\n```\n### 🔸 Select `Table` 'Exams' Columns from `Schema` 'Institute'\n```sql      \n      SELECT * FROM INSTITUTE.EXAMS;         -- Select Records from Schema (Institute), Table (Exams) = Schema.Table\n```\n### 🔸 Select `Table` 'Exams' Columns from `Schema` 'Institute' in `Database` 'FSA'\n```sql      \n      SELECT * FROM FSA.INSTITUTE.EXAMS;         -- Select Records from Database (FSA), Schema (Institute), Table (Exams) = Database.Schema.Table\n```\n### 🔸 Select `Specific Columns` from a Table 'Student'\n```sql      \n      SELECT Adm_No, Stud_Name, Class, Fee \n      FROM EXAMS;\n```\n### 🔸 Select Records with a Arithmetic Operators` (+, -, *, /, %)\n```sql      \n      SELECT Stud_Name, Fee, Fee + 50 AS Increased_Fee\n      FROM STUDENT;                                            -- Addition (+): Increase the FEE by 50 for each student\n```\n```sql      \n      SELECT Stud_Name, Fee, Fee - 100 AS Reduced_Fee\n      FROM STUDENT;                                            -- Subtraction (-): Decrease 100 from the Fee for each student\n```\n```sql      \n      SELECT Stud_Name, Fee, Fee * 2 AS Doubled_Fee\n      FROM STUDENT;                                            -- Multiplication (*): Multiply the Fee by 2 for each student\n```\n```sql      \n      SELECT Stud_Name, Fee, Fee / 2 AS Halved_Fee\n      FROM STUDENT;                                            -- Division (/): Divide the Fee by 2 for each student\n```\n```sql      \n      SELECT Stud_Name, Fee, Fee % 100 AS Remainder_Fee\n      FROM STUDENT;                                            -- Modulus (%): Find the remainder when Fee is divided by 100\n```\n### 🔸 Select Records with a `Condition` (`Comparison Operators`: =, \u003e, \u003c, \u003e=, \u003c=, !=, \u003c\u003e, !\u003c, !\u003e)\n```sql      \n      SELECT * FROM STUDENT\n      WHERE Class = 10;                     -- Return all records from STUDENT Table for 10th Class\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE Class \u003c\u003e 5;                     -- Return all records from STUDENT Table for all classes EXCEPT 5th Class [Class not equal to (\u003c\u003e or !=) 5]\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE Fee \u003e= 350;                     -- Return all records from STUDENT Table for those whose Fee more than or equal to 350\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE Fee !\u003e 350;                     -- Return all records from STUDENT Table for those whose Fee not more than 350\n```\n### 🔸 Select Records with Conditions by `Logical Operators`: AND, OR, NOT, IN, BETWEEN, LIKE, ANY/SOME, ALL, IS NULL\n```sql      \n      SELECT * FROM STUDENT\n      WHERE Fee \u003e 300 AND Class = 8;        -- Return all records from STUDENT Table for 8th Class whose Fee more than 300\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE Class = 10 OR Class = 9;         -- Return all records from STUDENT Table for 8th Class whose Fee more than 300\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE NOT Fee \u003e 350 AND Gender = 'F';   -- Return all records from STUDENT Table whose fee not more than 350 for Females\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE DOJ BETWEEN '2021-10-01' AND '2021-12-04';                 -- Return all records from STUDENT Table who joined between date range\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE Stud_Name LIKE 'Ru%' OR Stud_Name LIKE '%oon';             -- Used 'WILDCARD %' here\n```\n```sql\n      SELECT * FROM EXAMS\n      WHERE Subject_Name IN('Science', 'English', 'Computer') AND Marks_Obtained \u003e 90;    -- Return all records from EXAMS Table who get marks above 90 IN Science, English and Computer\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE Fee \u003e ANY (SELECT Fee FROM STUDENT WHERE Class = 8);        -- Find students whose Fee is greater than ANY student in Class 8\n```\n```sql\n      SELECT * FROM STUDENT                                              -- ANY=SOME: SOME is functionally equivalent to ANY in SQL\n      WHERE Fee = SOME (SELECT Fee FROM STUDENT WHERE Class = 8);        -- Find students whose Fee is equal to SOME student in Class 8\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE Fee \u003e ALL (SELECT Fee FROM STUDENT WHERE Class = 8);         -- Find students whose Fee is greater than ALL students in Class 8\n```\n```sql\n      SELECT Stud_Name FROM STUDENT                                      -- Find students whose Fee is greater than ALL students in Class 8\n      WHERE EXISTS (SELECT * FROM STUDENT WHERE Class = 10);             -- If an entry EXISTS, it returns the student's name\n```\n```sql\n      SELECT * FROM EXAMS\n      WHERE Marks_Obtained IS NULL;                                     -- Find students whose Marks_Obtained IS NULL (if any such values exist)\n```\n\n### 🔸 Select Records with `NESTED Queries/SUBqueries`\n```sql      \n      SELECT * FROM EXAMS\n      WHERE Marks_Obtained \u003e (SELECT AVG(Marks_Obtained) FROM EXAMS);        -- Return all records greater than average marks\n```\n### 🔸 Select Records with `Order/Sorting` (ASC or DESC)\n```sql      \n      SELECT * FROM STUDENT\n      ORDER BY Stud_Name ASC;\n```\n```sql\n      SELECT * FROM STUDENT\n      ORDER BY Class DESC;\n```\n\n\n# 🔘 ${\\color{blue}WILDCARDS}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+------------------------------------------------------------------------+\n| It is used to search for patterns with LIKE clause within string data. |\n+------------------------------------------------------------------------+\n```\n\n### 🔹 Select Records with `Wildcard %`\n```sql      \n      SELECT * FROM STUDENT\n      WHERE Guardian_Name LIKE 'M%';        -- Return all records from STUDENT Table where Guardian Name STARTS with letter 'M'\n```\n```sql\n      SELECT * FROM EXAMS\n      WHERE Subject_Name LIKE '%e';         -- Return all records from EXAMS Table where Subject Name ENDS with letter 'e'\n```\n```sql\n      SELECT * FROM EXAMS\n      WHERE Subject_Name LIKE 'M%s';        -- Return all records from EXAMS Table where Subject name STARTS with letter 'M' and ENDS with letter 's'\n```\n```sql\n      SELECT * FROM EXAMS\n      WHERE Addres LIKE '%ur%';             -- Return all records from EXAMS Table where Addres CONTAINS phrase 'ur'\n```\n\n### 🔹 Select Records with `Wildcard _`\n```sql      \n      SELECT * FROM STUDENT\n      WHERE Addres LIKE '_hilmil';           -- Return all records from STUDENT Table where Addres STARTS with ANY ONE character, FOLLOWED \"hilmil\"\n```\n```sql\n      SELECT * FROM EXAMS\n      WHERE Subject_Name LIKE 'Englis_';     -- Return all records from EXAMS Table where subject name STARTS with \"Englis\", ENDS with ANY ONE character\n```\n```sql\n      SELECT * FROM EXAMS                    -- Return all records from EXAMS Table where subject name STARTS with ANY 2 characters....\n      WHERE Subject_Name LIKE '__gl___';     -- FOLLOWED by \"gl\" and ENDS with ANY 3 characters\n```\n```sql\n      SELECT * FROM EXAMS                    -- Return all records from EXAMS Table where subject code starts with \"S\"....\n      WHERE Subject_Code LIKE 'S__002';      -- followed by any 2 characters and ends with '002'\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE Addres LIKE '_a%';               -- Return all records from STUDENT Table where addres starts with any one character, 'a' at 2nd position\n```\n\n### 🔹 Select Records with `Wildcard []`\n```sql      \n      SELECT * FROM STUDENT\n      WHERE Addres LIKE '[a-g]%';            -- Return all records from STUDENT Table where Addres starts with any one letter \"from 'a' to 'g'\" (a,b,c,d,e,f OR g)\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE Stud_Name LIKE 'A[nr]%'          -- Matches names starting with \"An\" or \"Ar\"\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE Guardian_Name LIKE '[rhs]%';    -- Return all records from STUDENT Table where Guardian Name STARTS with letter 'r' or 'h' or 's'\n```\n```sql\n      SELECT * FROM STUDENT\n      WHERE Guardian_Name LIKE '[^rhs]%';    -- Return all records from STUDENT Table where Guardian Name NOT STARTS with letter 'r' or 'h' or 's'\n```\n\n\n# 🔘 ${\\color{blue}CLAUSES}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+------------------------------------------------------------------------+\n| It is used to specify conditions or actions to be applied to the data. |\n| Clauses help to filter, group, sort, or limit the results of a query.  |\n+------------------------------------------------------------------------+\n```\n\n### 🔹 Select Records using `WHERE`\n- **`WHERE` clause: `Filter rows` based on a condition before grouping.**\n```sql\n       SELECT * FROM STUDENT\n       WHERE Class = 8;                                      -- Retrieve all students in Class 8\n```\n### 🔹 Select Records using `ORDER BY`\n- **`ORDER BY` clause: `Sort` the result set.**\n```sql\n       SELECT * FROM STUDENT\n       ORDER BY Stud_Name ASC;                               -- Retrieve all students sorted by Stud_Name in ascending order\n```\n```sql\n       SELECT * FROM STUDENT\n       WHERE Gender = 'M'\n       ORDER BY Stud_Name ASC;                               -- Retrieve all male students sorted by Stud_Name in ascending order\n```\n\n### 🔹 Select Records using `GROUP BY`\n- **`GROUP BY` clause: `Group rows` based on one or more columns.**\n```sql\n       SELECT Class, SUM(Fee) AS [Total Fee by Class]\n       FROM STUDENT\n       GROUP BY Class;                                       -- Sum the fee of students in each class\n```\n```sql\n       SELECT Class, SUM(Fee) AS [Total Fee by Class]\n       FROM STUDENT\n       WHERE Fee \u003e 300                                       -- Filters rows where Fee is greater than 300\n       GROUP BY Class\n       ORDER BY Class DESC;                                  -- Sum the fee of students in each class whose fee more than 300 sorted by Class in Descending order\n```\n\n### 🔹 Select Records using `HAVING`\n- **`HAVING` clause: `Filter groups` after Grouping to filter results on Aggregation.**\n```sql\n       SELECT Class, COUNT(*) AS NumberOfStudents\n       FROM STUDENT\n       GROUP BY Class\n       HAVING COUNT(*) \u003e 2;                                  -- Find classes with more than 2 students\n```\n```sql\n       SELECT Class, SUM(Fee) AS [Total Fee by Class]\n       FROM STUDENT\n       GROUP BY Class\n       HAVING SUM(Fee) \u003e 500                                 -- Filters groups where the total Fee is greater than 500\n       ORDER BY Class DESC;\n```\n\n### 🔹 Select Records using `TOP/OFFSET/FETCH`\n- **`TOP` clause: `Limit` the number of rows returned.**\n```sql\n       SELECT TOP 5 * FROM STUDENT\n       ORDER BY Fee DESC;                                    -- Retrieve the top 5 students by Fee in descending order\n```\n```sql\n       SELECT TOP 5 Adm_No, Stud_Name FROM STUDENT\n       ORDER BY Fee DESC;                                    -- Retrieve the top 5 students table Adm_No and Stud_Name columns by Fee in descending order\n```\n```sql\n       SELECT Adm_No, Marks_Obtained FROM EXAMS\n       ORDER BY Marks_Obtained DESC\n       OFFSET 3 ROWS                                         -- Skip (Offset) 1st 3 rows and then return next 5 rows\n       FETCH NEXT 5 ROWS ONLY;                               -- Retrieve the top 5 EXAMS table Adm_No and Marks_Obtained columns by Marks_Obtained in descending order\n```\n\n### 🔹 Select Records using `DISTINCT`\n- **`DISTINCT` clause: Select `unique` values only.**\n```sql\n       SELECT DISTINCT Class FROM STUDENT;                   -- Retrieve the distinct/unique classes of students\n```\n```sql\n       SELECT DISTINCT Class FROM STUDENT\n       WHERE Fee \u003e 300;                                      -- Retrieve the distinct classes/unique of students where student fee more than 300\n```\n\n### 🔹 Select Records using `JOIN`\n- **`JOIN` clause: Combines rows from two or more tables.**\n```sql\n       SELECT S.*, E.Subject_Name, E.Marks_Obtained          -- Fetch all columns (*) from Student Table and Subject \u0026 Marks obtained from Exams Table\n       FROM STUDENT S\n       INNER JOIN EXAMS E\n       ON S.Adm_No = E.Adm_No;\n```\n\n\n# 🔘 ${\\color{blue}JOIN_S}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+---------------------------------------------------------------------------------------------+\n| It is used to combine rows from two or more tables, based on a related column between them. |\n+---------------------------------------------------------------------------------------------+\n```\n\n### 🔸 Fetch Student Records using `(INNER) JOIN`\n- **`(INNER) JOIN`: Returns records that have only MATCHING (Common) VALUES IN BOTH TABLES.**\n```sql\n      SELECT S.Adm_No, S.Stud_Name, E.Subject_Name, E.Marks_Obtained\n      FROM STUDENT S\n      INNER JOIN EXAMS E\n      ON S.Adm_No = E.Adm_No;                             -- Returns the Admission No., student names along with the subjects and marks they obtained in the exams\n```\n```sql\n      SELECT S.Adm_No, S.Stud_Name, E.Subject_Name, E.Marks_Obtained\n      FROM STUDENT S\n      JOIN EXAMS E                                        -- JOIN or Inner JOIN works similarly\n      ON S.Adm_No = E.Adm_No\n      WHERE S.Class = 10;                                 -- Returns the students of class 10 and their respective exam marks\n```\n```sql\n      SELECT S.Adm_No, S.Stud_Name, E.Subject_Name, E.Marks_Obtained\n      FROM STUDENT S\n      INNER JOIN EXAMS E\n      ON S.Adm_No = E.Adm_No\n      WHERE E.Marks_Obtained \u003e 85;                        -- Fetches students who scored more than 85 marks in any exam\n```\n\n### 🔸 Fetch Student Records using `LEFT (OUTER) JOIN`\n- **`LEFT (OUTER) JOIN`: Returns all records from the LEFT TABLE, and the matched records from the RIGHT TABLE.**\n```sql\n      SELECT S.Adm_No, S.Stud_Name, E.Subject_Name, E.Marks_Obtained\n      FROM STUDENT S\n      LEFT JOIN EXAMS E\n      ON S.Adm_No = E.Adm_No;           -- Fetches all students, including those who may not have appeared in any exams\n```\n```sql\n      SELECT S.Adm_No, S.Stud_Name, E.Subject_Name, E.Marks_Obtained\n      FROM STUDENT S\n      LEFT OUTER JOIN EXAMS E           -- LEFT OUTER JOIN acts as similar to LEFT JOIN\n      ON S.Adm_No = E.Adm_No;\n```\n\n### 🔸 Fetch Student Records using `RIGHT (OUTER) JOIN`\n- **`RIGHT (OUTER) JOIN`: Returns all records from the RIGHT TABLE, and the matched records from the LEFT TABLE.**\n```sql\n      SELECT S.Adm_No, S.Stud_Name, E.Subject_Name, E.Marks_Obtained\n      FROM STUDENT S\n      RIGHT JOIN EXAMS E\n      ON S.Adm_No = E.Adm_No;           -- Fetches all exam records, even if some students may not exist in the STUDENT table (Null Values for Marks_Obtained)\n```\n```sql\n      SELECT S.Adm_No, S.Stud_Name, E.Subject_Name, E.Marks_Obtained\n      FROM STUDENT S\n      RIGHT OUTER JOIN EXAMS E\n      ON S.Adm_No = E.Adm_No;           -- RIGHT OUTER JOIN acts as similar to RIGHT JOIN\n```\n\n### 🔸 Fetch Student Records using `FULL (OUTER) JOIN`\n- **`FULL (OUTER) JOIN`: Returns all records when there is a match in either LEFT or RIGHT TABLE.**\n```sql\n      SELECT S.Adm_No, S.Stud_Name, E.Subject_Name, E.Marks_Obtained\n      FROM STUDENT S\n      FULL OUTER JOIN EXAMS E\n      ON S.Adm_No = E.Adm_No;           -- Fetches all exam records, even if some students may not exist in the STUDENT table (Null Values)\n```\n```sql\n      SELECT S.Adm_No, S.Stud_Name, E.Subject_Name, E.Marks_Obtained\n      FROM STUDENT S\n      FULL JOIN EXAMS E\n      ON S.Adm_No = E.Adm_No;           --  FULL JOIN acts as similar to FULL OUTER JOIN\n```\n  \n### 🔸 Fetch Student Records using `CROSS JOIN`\n- **`CROSS JOIN`: It combines every row of the first table with every row of the second table. `Example`: Cross join students with their subjects.**\n```sql\n      SELECT S.Stud_Name, E.Subject_Name\n      FROM STUDENT S\n      CROSS JOIN EXAMS E;              -- Pairs every student with every subject, producing a large set of combinations\n```\n\n### 🔸 Fetch Student Records using `SELF JOIN`\n- **`SELF JOIN`: A self join is when a table is joined with itself. It is useful for hierarchical or recursive data structures.**\n```sql\n\t\t\t\t    -- Find Students with the Same Guardian\n      SELECT \n\t    A.Stud_Name AS Student1, \n\t    B.Stud_Name AS Student2, \n    \t    A.Guardian_Name\n      FROM \n\t    STUDENT A\n      JOIN \n            STUDENT B \n      ON \n    \t    A.Guardian_Name = B.Guardian_Name \n    \t    AND A.Stud_Name \u003c B.Stud_Name;\n```\n```sql\n\t\t\t\t    -- Find Students and Their Monitor\n      SELECT \n\t    A.Stud_Name AS Student, \n    \t    B.Stud_Name AS Monitor\n      FROM \n\t    STUDENT A\n      JOIN \n            STUDENT B \n      ON \n    \t    A.Monitor_ID = B.Adm_No;\n```\n```sql\n\t\t\t\t    -- Find Students in the Same Class\n      SELECT \n\t    A.Stud_Name as Student1, \n    \t    B.Stud_Name as Student2,\n\t    A.Class\n      FROM \n\t    STUDENT A\n      JOIN \n            STUDENT B \n      ON \n    \t    A.Class = B.Class\n\t    AND A.Stud_Name \u003c B.Stud_Name;\n```\n```sql\n\t\t\t\t    -- Find Students with the Same Address\n      SELECT \n\t    A.Stud_Name as Student1, \n    \t    B.Stud_Name as Student2,\n\t    A.Addres\n      FROM \n\t    STUDENT A\n      JOIN \n            STUDENT B \n      ON \n    \t    A.Addres = B.Addres\n\t    AND A.Stud_Name \u003c B.Stud_Name;\n```\n\n### 🔸 Fetch Student Records using `UNION`\n- **`UNION`: It Combines the results of two or more SELECT statements. The UNION operator only returns DISTINCT records from one or more tables. It is useful when both the select statement have same number of columns and same kind of data types, either from one table or from two tables**\n```sql\n      SELECT Adm_No FROM STUDENT\n      UNION\n      SELECT Monitor_ID FROM STUDENT;   \t-- Return the DISTINCT values combination from 'Adm_No' and 'Monitor_ID' from same table\n```\n```sql\n      SELECT Adm_No FROM STUDENT\n      UNION\n      SELECT Adm_No FROM EXAMS;   \t\t-- Return the DISTINCT values combination from 'Adm_No' from two tables\n```\n### 🔸 Fetch Student Records using `UNION ALL`\n- **`UNION ALL`: It Combines the results of two or more SELECT statements. The UNION ALL operator returns Selected values including DUPLICATES from one or more tables. It is useful when both the select statement have same number of columns and same kind of data types, either from one table or from two tables**\n```sql\n      SELECT Adm_No FROM STUDENT\n      UNION ALL\n      SELECT Monitor_ID FROM STUDENT;   \t-- Return ALL values including DUPLICATES combination from 'Adm_No' and 'Monitor_ID' from same table\n```\n```sql\n      SELECT Adm_No FROM STUDENT\n      UNION ALL\n      SELECT Adm_No FROM EXAMS;   \t\t-- Return ALL values including DUPLICATES combination from 'Adm_No' from two tables\n```\n### 🔸 Fetch Student Records using `INTERSECT`\n- **`INTERSECT`: It Combines the results of two or more SELECT statements. The INTERSECT operator returns only COMMON values from one or more tables. It is useful when both the select statement have same number of columns and same kind of data types, either from one table or from two tables**\n```sql\n      SELECT Adm_No FROM STUDENT\n      INTERSECT\n      SELECT Monitor_ID FROM STUDENT;   \t-- Return COMMON values from 'Adm_No' and 'Monitor_ID' from same table\n```\n```sql\n      SELECT Adm_No FROM STUDENT\n      INTERSECT\n      SELECT Adm_No FROM EXAMS;   \t\t-- Return COMMON values from 'Adm_No' from two tables\n```\n\n\n# 📗 DCL (`Data Control Language`)\n\n\n# 🔘 ${\\color{blue}GRANT}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+----------------------------------------------------------+\n| It is used to give a user access rights to the database. |\n+----------------------------------------------------------+\n```\n  \n### 🔹 Grant `SELECT` Permission on a Table\n```sql      \n      GRANT SELECT ON STUDENT TO UserA;                      -- Gives the user 'UserA' the ability to perform SELECT queries on the STUDENT table\n```\n\n### 🔹 Grant `INSERT` and `UPDATE` Permissions on a Table\n```sql      \n      GRANT INSERT, UPDATE ON STUDENT TO UserA;              -- Allows UserA to insert new records and update existing ones in the STUDENT table\n```\n\n\n# 🔘 ${\\color{blue}REVOKE}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+----------------------------------------------------------------------------------+\n| It is used to remove access rights from the database which is granted to a user. |\n+----------------------------------------------------------------------------------+\n```\n\n### 🔸 Revoke `SELECT` Permission on a Table\n```sql      \n      REVOKE SELECT ON STUDENT TO UserA;                      -- Removes the SELECT permission, so UserA can no longer query data from the STUDENT table\n```\n\n### 🔸 Revoke `INSERT` and `UPDATE` Permissions on a Table\n```sql      \n      REVOKE INSERT, UPDATE ON STUDENT TO UserA;              -- Removes the ability for UserA to insert new records or update existing ones in the STUDENT table\n```\n\n# 📗 TCL (`Transaction Control Language`)\n\n\n# 🔘 ${\\color{blue}COMMIT}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+--------------------------------------------------------------------------------------+\n| It is used to save the current transaction permanently in the database.              |\n| Once a COMMIT is executed, the changes are made permanent and cannot be rolled back. |\n+--------------------------------------------------------------------------------------+\n```\n  \n### 🔹 Insert a Record and `Commit` the Transaction\n```sql      \n      BEGIN TRANSACTION;\n\n      INSERT INTO STUDENT (Adm_No, DOJ, Stud_Name, Gender, Guardian_Name, Address, Contact_Number, Class, Fee)\n      VALUES ('ROSE00332', '2023-09-15', 'John Doe', 'M', 'Richard Doe', 'Greenfield', '1234567890', 8, 350);\n\n      COMMIT;\n```\n\n\n# 🔘 ${\\color{blue}ROLLBACK}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+-------------------------------------------------------------+\n| It is used to Undo changes made in the current transaction. |\n+-------------------------------------------------------------+\n```\n  \n### 🔸 Insert a Record, but `Rollback` the Transaction\n```sql      \n      BEGIN TRANSACTION;\n\n      INSERT INTO STUDENT (Adm_No, DOJ, Stud_Name, Gender, Guardian_Name, Address, Contact_Number, Class, Fee)\n      VALUES ('ROSE00333', '2023-09-15', 'Jane Smith', 'F', 'John Smith', 'Blueville', '9876543210', 7, 300);\n\n      ROLLBACK;                                                                      -- If something goes wrong, rollback the transaction\n```\n\n\n# 🔘 ${\\color{blue}SAVEPOINT}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+----------------------------------------------------------------------------------+\n| It is used to set a point within a transaction to which you can roll back later. |\n+----------------------------------------------------------------------------------+\n```\n  \n### 🔹 Using `SAVEPOINT` in a Transaction\n```sql      \n      BEGIN TRANSACTION;\n\n      INSERT INTO STUDENT (Adm_No, DOJ, Stud_Name, Gender, Guardian_Name, Address, Contact_Number, Class, Fee)\n      VALUES ('ROSE00334', '2023-09-15', 'Alice Brown', 'F', 'Sam Brown', 'Redtown', '1122334455', 6, 250);\n\n      SAVEPOINT Save1;\n\n      INSERT INTO STUDENT (Adm_No, DOJ, Stud_Name, Gender, Guardian_Name, Address, Contact_Number, Class, Fee)\n      VALUES ('ROSE00335', '2023-09-15', 'Bob White', 'M', 'Jim White', 'Greenville', '5566778899', 5, 275);\n\n      ROLLBACK TRANSACTION Save1;                                                  -- Something goes wrong, rollback to the savepoint\n\n      COMMIT;                                                                      -- Now commit the first insert, but not the second\n```\n\n\n# 🔘 ${\\color{blue}SET\\ TRANSACTION}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+------------------------------------------------------------------------------------+\n| It is used to specify characteristics for the transaction (e.g., isolation level). |\n+------------------------------------------------------------------------------------+\n```\n  \n### 🔸 Using `SET TRANSACTION ISOLATION LEVEL`\n```sql      \n      SET TRANSACTION ISOLATION LEVEL READ COMMITTED;\n\n      BEGIN TRANSACTION;\n                                                                                           -- Query or Insert/Update/Delete operations                                                      \n\n      INSERT INTO STUDENT (Adm_No, DOJ, Stud_Name, Gender, Guardian_Name, Address, Contact_Number, Class, Fee)\n      VALUES ('ROSE00336', '2023-09-15', 'Charlie Green', 'M', 'Paul Green', 'Bluefield', '9988776655', 9, 400);\n\n      COMMIT;\n```\n\n# 📗 SFL (`Scalar Functions Library`)\n\n\n# 🔘 ${\\color{blue}STRING\\ FUNCTIONS}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+------------------------------------------------------------------------------------------------+\n| It is used to manipulate string data for tasks like concatenation, extraction, and formatting. |\n+------------------------------------------------------------------------------------------------+\n```\n  \n### 🔹 Concatenate `Adm_No` and `Stud_Name`\n```sql      \n      SELECT CONCAT(Adm_No, ' - ', Stud_Name) AS StudentDetails\n      FROM STUDENT;\n```\n\n### 🔹 Concatenate `Stud_Name` and `Guardian_Name`\n```sql      \n      SELECT CONCAT('Name: ', Stud_Name, ', Guardian: ', Guardian_Name) AS [Full Details]\n      FROM STUDENT;\n```\n  \n### 🔸 Extract `first 3 characters` of Stud_Name\n```sql      \n      SELECT SUBSTRING(Stud_Name, 1, 3) AS ShortName\n      FROM STUDENT;\n```\n\n### 🔸 Extract `4 characters` from Adm_No starting at `position 5`\n```sql      \n      SELECT SUBSTRING(Adm_No, 5, 4) AS PartAdmNo\n      FROM STUDENT;\n```\n  \n### 🔹 Find the length of `Stud_Name` (LEN)\n```sql      \n      SELECT Stud_Name, LEN(Stud_Name) AS NameLength\n      FROM STUDENT;\n```\n\n### 🔹 Find the length of `Address` (LEN)\n```sql      \n      SELECT Address, LEN(Address) AS AddressLength\n      FROM STUDENT;\n```\n\n### 🔹 Find the position of `Md` in Stud_Name (CHARINDEX)\n```sql      \n      SELECT CHARINDEX('Md', Stud_Name) AS Md_Position\n      FROM STUDENT;\n```\n\n### 🔹 Find the `starting position` of pattern `R` in Stud_Name (PATINDEX)\n```sql      \n      SELECT PATINDEX('%R%', Stud_Name) AS R_Position\n      FROM STUDENT;\n```\n  \n### 🔸 Convert Stud_Name to `UPPERCASE`\n```sql      \n      SELECT UPPER(Stud_Name) AS UpperCaseName\n      FROM STUDENT;\n```\n\n### 🔸 Convert Address to `lowercase`\n```sql      \n      SELECT LOWER(Address) AS LowerCaseAddress\n      FROM STUDENT;\n```\n  \n### 🔹 Remove `spaces` from Contact_Number\n```sql      \n      SELECT TRIM(Contact_Number) AS TrimmedContact\n      FROM STUDENT;\n```\n\n### 🔹 Remove `leading spaces` from Stud_Name\n```sql      \n      SELECT LTRIM(Stud_Name) AS Trimmed_Left_Name\n      FROM STUDENT;\n```\n\n### 🔹 Remove `trailing spaces` from Stud_Name\n```sql      \n      SELECT RTRIM(Stud_Name) AS Trimmed_Right_Name\n      FROM STUDENT;\n```\n  \n### 🔸 Replace 'Md' with 'Mr.' in Stud_Name\n```sql      \n      SELECT REPLACE(Stud_Name, 'Md', 'Mr.') AS Updated_Name\n      FROM STUDENT;\n```\n\n### 🔸 Replace hyphen `-` with `space` in Adm_No\n```sql      \n      SELECT REPLACE(Adm_No, '-', ' ') AS ModifiedAdmNo\n      FROM STUDENT;\n```\n\n### 🔸 Insert `Dr.` at the beginning of Stud_Name\n```sql      \n      SELECT STUFF(Stud_Name, 1, 0, 'Dr. ') AS Stuffed_Name\n      FROM STUDENT;\n```\n  \n### 🔹 Extract `first 5` characters of Adm_No\n```sql      \n      SELECT LEFT(Adm_No, 5) AS FirstFive\n      FROM STUDENT;\n```\n\n### 🔹 Extract `last 4` characters of Contact_Number\n```sql      \n      SELECT RIGHT(Contact_Number, 4) AS LastFourDigits\n      FROM STUDENT;\n```\n  \n### 🔸 Reverse `Adm_No`\n```sql      \n      SELECT REVERSE(Adm_No) AS ReversedAdmNo               -- reverse the order of characters in a string\n      FROM STUDENT;\n```\n\n### 🔸 Reverse `Stud_Name`\n```sql      \n      SELECT REVERSE(Stud_Name) AS ReversedName             -- reverse the order of characters in a string\n      FROM STUDENT;\n```\n  \n### 🔹 Repeat '*' symbol 5 times\n```sql      \n      SELECT REPLICATE('*', 5) AS Stars                      -- Return star (*) 5 times\n      FROM STUDENT;\n```\n\n### 🔹 Repeat `Stud_Name` twice\n```sql      \n      SELECT REPLICATE(Stud_Name, 2) AS DoubledName          -- Return the same Student Name 2 times\n      FROM STUDENT;\n```\n### 🔹 Repeat String like a `Loop`\n```sql      \n      PRINT 'This is a loop';\n      GO 5                           -- 'GO' is not an actual SQL command, It is a part of SSMS which indicates end of Batch (group of commands) in SSMS\n/*\nOUTPUT:\n=======\nBeginning execution loop\nThis is a loop\nThis is a loop\nThis is a loop\nThis is a loop\nThis is a loop\nBatch execution completed 4 times.\n*/\n```\n  \n### 🔸 Format Contact_Number with `dashes`\n```sql      \n      SELECT FORMAT(Contact_Number, '###-###-####') AS FormattedContact   -- format a value according to a format\n      FROM STUDENT;\n```\n\n### 🔸 Format DOJ in `MM/dd/yyyy` format\n```sql      \n      SELECT FORMAT(DOJ, 'MM/dd/yyyy') AS FormattedDOJ\n      FROM STUDENT;\n```\n\n# 🔘 ${\\color{blue}MATHEMATICAL\\ FUNCTIONS}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+---------------------------------------------------------------------------------------------------------------------+\n| It is used to perform basic mathematical operations like Square root, Round, Power, Floor, Ceiling on numeric data. |\n+---------------------------------------------------------------------------------------------------------------------+\n```\n  \n### 🔹 `ABS()` : Returns the absolute (`Positive`) value of a number\n```sql\n\t     \t\t\t\t\t\t-- Inside the (), Can be used COLUMN Name of a Table\n      SELECT ABS(-10) AS AbsoluteValue;             \t-- Returns the absolute value of -10\n```\n### 🔹 `ROUND()` : Rounds a number to a specified number of decimal places\n```sql\n\t      \t\t\t\t\t\t-- Inside the (), Can be used COLUMN Name of a Table\n      SELECT ROUND(12.3456, 2) AS RoundedValue;      \t-- Rounds 12.3456 to 2 decimal places\n```\n### 🔹 `SQRT()` : Returns the square root of a number\n```sql\n\t      \t\t\t\t\t\t-- Inside the (), Can be used COLUMN Name of a Table\n      SELECT SQRT(25) AS SquareRoot;      \t\t-- Returns the square root of 25\n```\n### 🔹 `CEILING()` : Returns the smallest integer greater than or equal to a number\n```sql\n\t      \t\t\t\t\t\t-- Inside the (), Can be used COLUMN Name of a Table\n      SELECT CEILING(4.3) AS CeilingValue;      \t-- Returns the ceiling of 4.3 (next whole number, round up forwards)\n```\n### 🔹 `FLOOR()` : Returns the largest integer less than or equal to a number\n```sql\n\t      \t\t\t\t\t\t-- Inside the (), Can be used COLUMN Name of a Table\n      SELECT FLOOR(4.7) AS FloorValue;      \t\t-- Returns the floor of 4.7 (previous whole number, round up backwards)\n```\n### 🔹 `POWER()` : Raises a number to the power of another number\n```sql\n\t      \t\t\t\t\t\t-- Inside the (), Can be used COLUMN Name of a Table\n      SELECT POWER(2, 3) AS PowerValue;      \t\t-- Raises 2 to the power of 3 (2^3)\n```\n### 🔹 `EXP()` : Returns e raised to the power of a specified number\n```sql\n\t      \t\t\t\t\t\t-- Inside the (), Can be used COLUMN Name of a Table\n      SELECT EXP(1) AS ExponentialValue;      \t\t-- Returns e^1 (where e is Euler's number, approximately 2.718)\n```\n### 🔹 LOG() : Returns the natural logarithm (base e) of a number\n```sql\n      \t\t\t\t\t\t\t-- Inside the (), Can be used COLUMN Name of a Table\n      SELECT LOG(10) AS LogarithmValue;      \t\t-- Returns the natural logarithm of 10\n```\n\n# 🔘 ${\\color{blue}DATE\\ FUNCTIONS}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+-----------------------------------------------------------+\n| It is used to handle and manipulate date and time values. |\n+-----------------------------------------------------------+\n```\n  \n### 🔹 `GETDATE()` : Returns the current date and time\n```sql\n      SELECT GETDATE() AS CurrentDateTime;\n```\n### 🔹 `YEAR()` : Extracts the year from a date\n```sql\n        SELECT YEAR(DOJ) AS YearOfJoining\n\tFROM STUDENT;\n```\n### 🔹 `MONTH()` : Extracts the month from a date\n```sql\n        SELECT MONTH(DOJ) AS MonthOfJoining\n\tFROM STUDENT;\n```\n### 🔹 `DAY()` : Extracts the day from a date\n```sql\n        SELECT DAY(DOJ) AS DayOfJoining\n\tFROM STUDENT;\n```\n### 🔹 `DATEADD()` : Adds a specified time interval to a date\n```sql\n        SELECT Adm_No, DOJ, DATEADD(DAY, 5, DOJ) AS DOJPlus5Days     -- Adds 5 days to the 'DOJ' (Date of Joining) column\n\tFROM STUDENT;\n```\n### 🔹 `DATEDIFF()` : Returns the difference between two dates in terms of a specified time interval\n```sql\n        SELECT Adm_No, DOJ, DATEDIFF(DAY, DOJ, GETDATE()) AS DaysSinceJoining     -- Returns the difference in days between 'DOJ' and the current date\n\tFROM STUDENT;    \n```\n\n# 🔘 ${\\color{blue}LOGICAL\\ FUNCTIONS}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+------------------------------------------+\n| It is used to handle the missing values. |\n+------------------------------------------+\n```\n  \n### 🔹 `ISNULL()` : Replaces NULL with a specified value `(with one Argument)`\n```sql\n      SELECT Adm_No, ISNULL(Monitor_ID, 'No Monitor') AS MonitorStatus          -- Replaces NULL in Monitor_ID with 'No Monitor'\n      FROM STUDENT;\n```\n### 🔹 `COALESCE()` : Returns the first non-null value from a list `(with multiple Arguments)`\n```sql\n      SELECT Adm_No, COALESCE(Guardian_Name, Monitor_ID, 'Not Available') AS FirstNonNullValue  \n      FROM STUDENT;                         -- Returns the first non-null value from Guardian_Name, Monitor_ID, or 'Not Available'\n```\n\n# 🔘 ${\\color{blue}CONVERSION\\ FUNCTIONS}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+-------------------------------------------------+\n| It is used to change data types of expressions. |\n+-------------------------------------------------+\n```\n  \n### 🔹 `CAST()` : Converts one data type to another\n```sql\n      SELECT CAST('2024-09-24' AS DATE) AS ConvertedDate;                  -- This converts a string to a date type\n```\n### 🔹 `CONVERT()` : Similar to CAST(), but allows formatting options\n```sql\n      SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS FormattedDate;        -- This converts the current date to a string formatted as MM/DD/YYYY\n```\n### 🔹 `TRY_CAST()` : Attempts conversion; returns `NULL` if it fails\n```sql\n      SELECT TRY_CAST('123.45' AS INT) AS ConvertedValue;            -- This tries to convert a string to an integer. Since it can't be converted, it returns NULL\n```\n### 🔹 `TRY_CONVERT()` : Similar to TRY_CAST(), with formatting options\n```sql\n      SELECT TRY_CONVERT(VARCHAR(10), 'NotANumber') AS SafeConvert;\n\t\t\t\t\t-- This attempts to convert a non-numeric string to a varchar. It returns NULL due to the failure in conversion\n```\n\n\n# 📗 WFL (`Windows Functions Library`)\n\n\n# 🔘 ${\\color{blue}AGGREGATE\\ FUNCTIONS}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+----------------------------------------------------------------------------------+\n| It is used to perform a calculation on Set of Values and return a single value. |\n| It is commonly used to summarize or analyze data.                                |\n+----------------------------------------------------------------------------------+\n```\n\n### 🔸 Select Records using `COUNT()`\n```sql      \n      SELECT COUNT(*) AS TotalStudents\n      FROM STUDENT;                          -- Count the total number of records/rows from student table\n```\n### 🔸 Select Records using `SUM()`\n```sql      \n      SELECT SUM(Fee) AS TotalFees\n      FROM STUDENT;                          -- Calculate the total fees of all students from student table\n```\n### 🔸 Select Records using `AVG()`\n```sql      \n      SELECT AVG(Fee) AS AverageFee\n      FROM STUDENT;                          -- Calculate the average fee of students from student table\n```\n### 🔸 Select Records using `MIN()`\n```sql      \n      SELECT MIN(Fee) AS MinimumFee\n      FROM STUDENT;                          -- Find the minimum fee paid by a student from student table\n```\n### 🔸 Select Records using `MAX()`\n```sql      \n      SELECT MAX(Fee) AS MinimumFee\n      FROM STUDENT;                          -- Find the maximum fee paid by a student from student table\n```\n### 🔸 Select Records using `COUNT()` with GROUP BY and HAVING clauses\n```sql      \n      SELECT Class, COUNT(*) AS NumberOfStudents\n      FROM STUDENT\n      GROUP BY Class\n      HAVING COUNT(*) \u003e 2;                   -- Find classes with more than 2 students\n```\n### 🔸 Check DUPLICATE Records in a Table using `COUNT()`\n```sql      \n      SELECT Adm_No, COUNT(*) as DuplicateCount\n      FROM STUDENT\n      GROUP BY Adm_No\n      HAVING COUNT(*) \u003e= 2;                   -- Find classes with more than 2 students\n```\n\n\n# 🔘 ${\\color{blue}RANKING\\ FUNCTIONS}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+--------------------------------------------------------------------------------------+\n| It’s commonly used for ranking results, Assigns a unique sequential integer to rows, |\n| such as determining the top or bottom records in a dataset. \t\t\t       |                                \n+--------------------------------------------------------------------------------------+\n```\n\n### 🔸 Assign a Rank to rows using `ROW_NUMBER()`\n```sql      \n       \tSELECT Adm_No, Marks_Obtained,\n        ROW_NUMBER() OVER (ORDER BY Marks_Obtained DESC) AS RowNum\n\tFROM EXAMS;\n```\n### 🔸 Assign a Rank to rows using `DENSE_RANK()` with no gaps even if any `TIES`\n```sql      \n       \tSELECT Adm_No, Marks_Obtained,\n        DENSE_RANK() OVER (ORDER BY Marks_Obtained DESC) AS DenseRank\n\tFROM EXAMS;\n```\n### 🔸 Assign a Rank to rows using `RANK()` with gaps if any `TIES`\n```sql      \n       \tSELECT Adm_No, Marks_Obtained,\n        RANK() OVER (ORDER BY Marks_Obtained DESC) AS Rank\n\tFROM EXAMS;\n```\n### 🔸 Divide rows using `NTILE()` into bucket of Grouped Sequence\n```sql      \n       \tSELECT Adm_No, Marks_Obtained,\n        NTILE(4) OVER (ORDER BY Marks_Obtained DESC) AS NTileGroup      -- If total 16 rows, NTILE(4) divides into groups of 4-4\n\tFROM EXAMS;                                                     -- with rank 1-1 for 1st four, 2-2 for next four, 3-3 for again next four, and 4-4 for last four\n```\n### 🔸 Display the `N-th Rank` marks obtained using `DENSE_RANK()` with `CTE`\n- **`CTE`: A CTE (Common Table Expression) is a temporary table you create in a SQL query to make it easier to write and understand complex queries.**\n```sql      \n       \tWith CTE_RankNo AS (\n\t\t             SELECT Adm_No, Marks_Obtained,\n\t\t\t     DENSE_RANK() OVER (ORDER BY Marks_Obtained DESC) AS [Rank Position]\n\t\t\t     FROM EXAMS\n\t\t           )\n\tSelect *\n\tFrom RankNo\n\tWhere [Rank Position] = 2;                 -- Similarly can display 3rd, 4th,......nth RANK by DESC from TOP \u0026 ASC from BOTTOM\n```\n### 🔸 Delete DUPLICATE Records using `ROW_NUMBER()` with `CTE`\n```sql      \n       With CTE_RemovDup AS (\n\t\t\t     SELECT *,\n        \t\t     ROW_NUMBER() OVER (PARTITION BY Adm_No, DOJ, Stud_Name \n                              \t\t\tORDER BY Adm_No) AS DupRecord\n\t\t\t     FROM STUDENT;\n\t\t            )\n\tDELETE FROM CTE_RemovDup\n\tWHERE DupRecord \u003e 1;\n```\n\n\n# 🔘 ${\\color{blue}ANALYTIC\\ FUNCTIONS}$\n🏠 [Home](https://github.com/ialam085/SQL_Server_Practice_All_Queries/blob/main/README.md#-colorblueclick-the-links-below-to-navigate-directly-to-the-desired-colorredsql-commands)\n```diff\n+-------------------------------------------------------------------------------+\n| It is used to compute values across a set of rows related to the current row, |\n| enabling analysis without grouping the data. \t\t\t       \t\t|                                \n+-------------------------------------------------------------------------------+\n```\n```sql\nTable: EXAMS1\n+----------+--------------+--------------+----------------+------------+\n|  Adm_No  | Subject_Code | Subject_Name | Marks_Obtained | Exam_Date  |\n+----------+--------------+--------------+----------------+------------+\n| ROSE0001 |\tSUB001\t  |    Math\t |\t85\t  | 2023-01-10 |\n| ROSE0001 |\tSUB002\t  |   Science\t |\t90\t  | 2023-01-20 |\n| ROSE0001 |\tSUB003\t  |   English\t |\t88\t  | 2023-01-30 |\n| ROSE0002 |\tSUB001\t  |    Math\t |\t78\t  | 2023-01-10 |\n| ROSE0002 |\tSUB002\t  |   Science\t |\t82\t  | 2023-01-20 |\n| ROSE0002 |\tSUB003\t  |   English\t |\t75\t  | 2023-01-30 |\n| ROSE0003 |\tSUB001\t  |    Math\t |\t92\t  | 2023-01-10 |\n| ROSE0003 |\tSUB002\t  |   Science\t |\t85\t  | 2023-01-20 |\n+----------+--------------+--------------+----------------+------------+\n```\n\n### 🔸 Using `CUME_DIST()` Calculate the cumulative distribution of marks obtained\n```sql      \n       \tSELECT Adm_No, Subject_Name, Marks_Obtained,\n        CUME_DIST() OVER (ORDER BY Marks_Obtained) AS CumulativeDistribution\n\tFROM EXAMS1;\n```\n### 🔸 Using `PERCENT_RANK()` Calculate the percent rank of each student's marks\n```sql      \n       \tSELECT Adm_No, Subject_Name, Marks_Obtained,\n        PERCENT_RANK() OVER (ORDER BY Marks_Obtained) AS PercentRank\n\tFROM EXAMS1;\n```\n### 🔸 Using `FIRST_VALUE()` Get the first mark obtained for each student\n```sql      \n       \tSELECT Adm_No, Subject_Name, Marks_Obtained, Exam_Date,\n        FIRST_VALUE(Marks_Obtained) OVER (PARTITION BY Adm_No ORDER BY Exam_Date) AS FirstMark\n\tFROM EXAMS1;\n```\n### 🔸 Using `LAST_VALUE()` Get the last mark obtained for each student\n```sql      \n       \tSELECT Adm_No, Subject_Name, Marks_Obtained, Exam_Date,\n        LAST_VALUE(Marks_Obtained) OVER (PARTITION BY Adm_No ORDER BY Exam_Date \n        ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS LastMark\n\tFROM EXAMS1;\n```\n### 🔸 Using `LAG()` Get the previous mark obtained by each student\n```sql      \n       \tSELECT Adm_No, Subject_Name, Marks_Obtained, Exam_Date,\n        LAG(Marks_Obtained) OVER (PARTITION BY Adm_No ORDER BY Exam_Date) AS PreviousMark\n\tFROM EXAMS1;\n```\n### 🔸 Using `LEAD()` Get the next mark obtained by each student\n```sql      \n       \tSELECT Adm_No, Subject_Name, Marks_Obtained, Exam_Date,\n        LEAD(Marks_Obtained) OVER (PARTITION BY Adm_No ORDER BY Exam_Date) AS NextMark\n\tFROM EXAMS;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fialam085%2Fa2z_sql_solutions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fialam085%2Fa2z_sql_solutions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fialam085%2Fa2z_sql_solutions/lists"}