1. What is database?

A database is an organized collection of data, stored and retrieved digitally from a remote or local computer system.

2. Datatype of MSSQ?

Numeric Types:

  • INT: Integer data type.
  • DECIMAL or NUMERIC: Fixed-point numeric data type.
  • FLOAT: Floating-point numeric data type.

Character and String Types:

  • CHAR: Fixed-length character data type.
  • VARCHAR: Variable-length character data type.
  • TEXT: Variable-length character data type for large text.

Date and Time Types:

  • DATE: Date data type.
  • TIME: Time data type.
  • DATETIME: Date and time data type.
  • SMALLDATETIME: Compact date and time data type.

Binary Types:

  • BINARY: Fixed-length binary data type.
  • VARBINARY: Variable-length binary data type.
  • IMAGE: Variable-length binary data type for large binary data.

Miscellaneous Types:

  • BIT: Boolean data type.
  • UNIQUEIDENTIFIER: Unique identifier (GUID) data type.
  • XML: XML data type.

Row Versioning Types:

  • TIMESTAMP (deprecated, replaced by ROWVERSION): Automatic unique binary numbers for versioning.

Large Object Types:

  • NTEXT: Variable-length Unicode character data type for large text.
  • NCHAR: Fixed-length Unicode character data type.
  • NVARCHAR: Variable-length Unicode character data type.
  • BLOB: Binary large object data type.

3. What is DBMS?

DBMS stands for Database Management System. DBMS is a system software responsible for the creation, retrieval, updation, and management of the database. It ensures that our data is consistent, organized, and is easily accessible by serving as an interface between the database and its end-users or application software.

  • It helps maintain data uniformity
  • Handles large sets of data efficiently (āĻĻāĻ•ā§āώāϤāĻžāϰ āϏāĻžāĻĨ⧇)
  • Versatile (āĻŦāĻšā§āĻŽā§āĻ–ā§€)
  • Faster way of managing data

4. What is RDBMS?

RDBMS stands for Relational Database Management System.

  • Allow multiple-user access
  • Store large packs of data
  • Minimum Data Redundancy (āĻ…āĻĒā§āϰāϝāĻŧā§‹āϜāύ⧀āϝāĻŧāϤāĻž)
  • Maintains Data Integration (āĻŽāĻŋāĻļā§āϰāĻŖ)
  • Better Tools for Structuring and Organizing Data

5. How is it different from DBMS?

The key difference here, compared to DBMS, is that RDBMS stores data in the form of a collection of tables, and relations can be defined between the common fields of these tables. Most modern database management systems like MySQL, Microsoft SQL Server, Oracle, IBM DB2, and Amazon Redshift are based on RDBMS.

  • RDBMS is the improved version of DBMS, and therefore, faster and more efficient than DBMS.
  • Use DBMS for basic data storage and retrieval; use RDBMS when you need structured data with relationships and want to enforce data integrity through features like transactions and ACID properties.
ParameterDBMSRDBMS
FunctionDBMS is system software for creating, storing, managing, updating and retrieving data from databases.RDBMS is software that allows the creation and management of databases in a tabular format for efficient retrieval, updation, and storage of data.
StorageIn DBMS, the storage of data is in the files. It is arranged either in a hierarchical form or navigational form.In RDBMS, the data is stored in tables. There is no hierarchy and instead, follow a relational model. Columns are the headers and rows contain the corresponding values.
Number of UsersDatabase Management System can only support a single user.Relational Database Management System allows access to multiple users to the databases.
Normalization (organizing data in the database)DBMS does not support normalization.Normalization is enabled in RDBMS. In fact, it was introduced by Edgar F. Codd for his relational database model.
Data TypeDBMS cannot store large quantities of data.RDBMS allows users to store a large set of data.
Data RelationshipsIn the database management system, there are no relationships amongst the data stored.In RDBMS, there are relationships formed amongst the data stored in tables.
Data FetchingThe process of data fetching in DBMS is slow.The process of Data Fetching is faster, and efficient in RDBMS because of its relational model.
Distributed DatabasesDBMS does not support the distribution of databases. A distributed database is a database that can be stored at different locations.RDBMS supports distributed databases.
Data RedundancyThe version of DBMS increases data redundancy (repetition of data).In RDBMS, data redundancy is eliminated that reduces wastage of time and resources.
Hardware and Software RequirementsDBMS needs minimum software and hardware requirements.In RDBMS, hardware and software requirements are higher than the classic DBMS.
Data IntegrityDatabase Management System does not support any data integration constraints or methods.The Relational Database Management System supports data integrity constraints.
Data AccessIn DBMS, you can access only a single file from a single database.In RDBMS, you can access multiple data at a single time.
Data SecurityDBMS is more prone to data theft, and access to unauthorized users because it does not allow any data security measures.RDBMS supports security measures and is more secure than the traditional RDBMS.
ACID PropertiesDBMS does not support any ACID properties.RDBMS supports ACID properties and ensures no data inconsistencies.
Data Client-ServerDBMS does not support client-server architecture.RDBMS supports client-server architecture
ExamplesXML, File systems, window registry, etc are some examples of database management systems.Oracle, MYSQL, SQL Server, etc are some of the examples of Relational Database Management Systems.

6. What is SQL?

SQL stands for Structured Query Language. It is the standard language for relational database management systems.

7. What are Tables and Fields?

A table is an organized collection of data stored in the form of rows and columns. Columns can be categorized as vertical and rows as horizontal. The columns in a table are called fields while the rows can be referred to as records.

8. What are Constraints in SQL?

Constraints are used to specify the rules concerning data in the table. It can be applied for single or multiple fields in an SQL table during the creation of the table or after creating using the ALTER TABLE command. The constraints are:

  • NOT NULL - Restricts NULL value from being inserted into a column.
  • CHECK - Verifies that all values in a field satisfy a condition.
  • DEFAULT - Automatically assigns a default value if no value has been specified for the field.
  • UNIQUE - Ensures unique values to be inserted into the field.
  • INDEX - Indexes a field providing faster retrieval of records.
  • PRIMARY KEY - Uniquely identifies each record in a table.
  • FOREIGN KEY - Ensures referential integrity for a record in another table.

9. What is a Primary Key?

The PRIMARY KEY constraint uniquely identifies each row in a table. It must contain UNIQUE values and has an implicit NOT NULL constraint.

10. What is a UNIQUE constraint?

A UNIQUE constraint ensures that all values in a column are different. This provides uniqueness for the column(s) and helps identify each row uniquely. Unlike primary key, there can be multiple unique constraints defined per table. The code syntax for UNIQUE is quite similar to that of PRIMARY KEY and can be used interchangeably.

AttributePrimary KeyUnique Key
UniquenessYes, and it must be unique for each record.Yes, but uniqueness is not always mandatory.
NULL valuesNo, primary key values cannot be NULL.Yes, unique key values can be NULL.
Number per TableOne per table.Multiple unique keys can exist in a table.
PurposeMain identifier for the table.Ensures uniqueness but not always the main identifier.
ConstraintsImplies uniqueness, non-null, and integrity constraints.Imposes uniqueness but may allow NULL values.

11. What is a Foreign Key?

A FOREIGN KEY comprises of single or collection of fields in a table that essentially refers to the PRIMARY KEY in another table. Foreign key constraint ensures referential integrity in the relation between two tables.

12. What is a Join? List its different types.

The SQL Join clause is used to combine records from two or more tables in a SQL database based on a related column between the two.

There are four different types of JOINs in SQL:

  • (INNER) JOIN: Retrieves records that have matching values in both tables involved in the join.
SELECT * FROM Table_A JOIN Table_B;
SELECT * FROM Table_A INNER JOIN Table_B;

  • LEFT (OUTER) JOIN: Retrieves all the records/rows from the left and the matched records/rows from the right table.
SELECT * FROM Table_A A LEFT JOIN Table_B B ON A.col = B.col;

  • RIGHT (OUTER) JOIN: Retrieves all the records/rows from the right and the matched records/rows from the left table.
SELECT * FROM Table_A A RIGHT JOIN Table_B B ON A.col = B.col;

  • FULL (OUTER) JOIN: Retrieves all the records where there is a match in either the left or right table.
SELECT * FROM Table_A A FULL JOIN Table_B B ON A.col = B.col;

There are also two joins available:

13. What are some common clauses used with SELECT query in SQL?

Some common SQL clauses used in conjunction with a SELECT query are as follows:

  • WHERE clause in SQL is used to filter records that are necessary, based on specific conditions.
  • ORDER BY clause in SQL is used to sort the records based on some field(s) in ascending (ASC) or descending order (DESC).
  • GROUP BY clause in SQL is used to group records with identical data and can be used in conjunction with some aggregation functions to produce summarized results from the database.
  • HAVING clause in SQL is used to filter records in combination with the GROUP BY clause. It is different from WHERE, since the WHERE clause cannot filter aggregated records.

14. What is an Alias (āωāĻĒāύāĻžāĻŽ) in SQL?

An alias is a feature of SQL that is supported by most, if not all, RDBMSs. It is a temporary name assigned to the table or table column for the purpose of a particular SQL query. In addition, aliasing can be employed as an obfuscation technique to secure the real names of database fields. A table alias is also called a correlation name.

15. Index in SQL:

A database structure that enhances data retrieval efficiency by providing a quick lookup of rows based on the values in one or more columns.

16. Clustered Index:

A type of index that determines the physical order of data rows in the table, and the table can have only one; it directly affects the order of data storage.

17. Non-Clustered Index:

A type of index that does not alter the physical order of data rows and creates a separate structure for quick data retrieval without changing the actual data storage order.

Normalization:

āϧāϰ⧁āύ āφāĻĒāύāĻŋ āĻāĻ•āϟāĻž āϕ⧋āύ⧋ āĻ“āϝāĻŧ⧇āĻŦ āϏāĻŋāĻ¸ā§āĻŸā§‡āĻŽ āĻŦāĻžāύāĻžāĻšā§āϛ⧇āύ āφāϰ āϤāĻžāϰ āϜāĻ¨ā§āϝ āĻĄāĻžāϟāĻžāĻŦ⧇āϏ āĻ¸ā§āĻŸā§āϰāĻžāĻ•āϚāĻžāϰ āĻŦāĻžāύāĻŋāϝāĻŧ⧇āϛ⧇āύ, āĻ•āĻŋāĻ¨ā§āϤ⧁ āϏ⧇āϟāĻžāϕ⧇ normalised āĻŦāĻžāύāĻžāύāύāĻŋ, āĻĢāϞ⧇ āĻĻ⧇āĻ–āĻŦ⧇āύ āϖ⧁āĻŦ āĻ•āĻŽ āĻĄāĻžāϟāĻž āĻāĻ¨ā§āĻŸā§āϰāĻŋ āĻ•āϰāĻžāϰ āĻĒāϰ⧇āχ āĻ…āύ⧇āĻ• āϜāĻžāϝāĻŧāĻ—āĻž āχāωāϜ āĻšāϝāĻŧ⧇ āϗ⧇āϛ⧇āĨ¤ āφāĻŦāĻžāϰ āĻĄāĻžāϟāĻžāĻŦ⧇āϏ āϟāĻž localhost āĻĨ⧇āϕ⧇ āϖ⧁āϞ⧇ āĻĻ⧇āĻ–āϤ⧇ āĻĒāĻžāϰāĻŦ⧇āύ āϝ⧇ āĻ•āĻŋāϛ⧁ āĻ•āĻŋāϛ⧁ āϜāĻžāϝāĻŧāĻ—āĻžāϝāĻŧ āĻāĻ•āχ āĻĢāĻŋāĻ˛ā§āĻĄ āĻŦāĻžāϰ āĻŦāĻžāϰ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻšāϝāĻŧ⧇āϛ⧇, āĻĢāϞ⧇ āϏ⧇āϟāĻž āĻ…āύ⧇āĻ• āĻĄāĻŋāĻ¸ā§āĻ• āĻ¸ā§āĻĒ⧇āϏ āύāĻˇā§āϟ āĻ•āϰ⧇āϛ⧇ āφāĻŦāĻžāϰ āĻĄāĻžāϟāĻž āϗ⧁āϞ⧋ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āĻŸā§‡āĻŦāĻŋāϞ⧇āϰ āĻŽāĻ§ā§āϝ⧇ āĻāĻĻāĻŋāϕ⧇ āĻ“āĻĻāĻŋāϕ⧇ āĻ›āĻĄāĻŧāĻŋāϝāĻŧ⧇ āφāϛ⧇ āĻŽāĻžāύ⧇ āχāĻ¨ā§āĻŸā§‡āĻ—ā§āϰāĻŋāϟāĻŋ āύ⧇āχ āĻŦāϞāϞ⧇āχ āϚāϞ⧇āĨ¤ āϤāĻžāχ āϝāĻ–āύāχ āφāĻŽāϰāĻž āĻĄāĻžāϟāĻžāĻŦ⧇āϏ āĻŦāĻžāύāĻžāχ āϤāĻ–āύ āĻāĻ•āϟāĻž āĻĒāĻĻā§āϧāϤāĻŋ āĻ…āύ⧁āϏāϰāĻŖ āĻ•āϰāĻŋ āϝāĻžāϤ⧇ āϏ⧇āχ āĻĄāĻžāϟāĻžāĻŦ⧇āϏ āϟāĻž āĻ…āύ⧇āĻ• āĻŦ⧇āĻļāĻŋ āĻ…āĻĒāϟāĻŋāĻŽāĻžāχāϜ āĻ“ āχāĻ¨ā§āϟāĻŋāĻ—ā§āϰ⧇āĻŸā§‡āĻĄ(āĻāĻ•āϏāĻžāĻĨ⧇ āϜ⧁āĻĄāĻŧ⧇ āĻĨāĻžāĻ•āĻžāϰ āĻ•ā§āώāĻŽāϤāĻž) āĻ“ āϏāĻŦāĻĨ⧇āϕ⧇ āĻ•āĻŽ āϰāĻŋāĻĄāĻžāύāĻĄā§‡āĻ¨ā§āϏāĻŋ(dublicate) āϰāĻžāĻ–āĻžāϰ āĻĻāĻŋāĻ• āϟāĻž āĻŽāĻžāĻĨāĻžāϝāĻŧ āϰ⧇āϖ⧇āĨ¤ Database normalisation āĻšāϞ⧋ database āϕ⧇ āϏāĻŦ āĻĨ⧇āϕ⧇ āĻŦ⧇āĻļāĻŋ dublicate āĻ­ā§āϝāĻžāϞ⧁ āĻāϰ āĻĨ⧇āϕ⧇ āĻŽā§āĻ•ā§āϤ āϰāĻžāĻ–āĻž, āφāϰ āĻāϟāĻž āĻ•āϰāĻž āĻšāϝāĻŧ āĻāĻ•āχ āĻĄāĻžāϟāĻžāĻŦ⧇āϏ⧇āϰ āĻ…āύ⧇āĻ• āĻŸā§‡āĻŦāĻŋāϞ⧇āϰ āĻŽāĻ§ā§āϝ⧇ āĻāĻ•āϟāĻž āĻ•āĻŽāύ āĻĢāĻŋāĻ˛ā§āĻĄ/āĻ•āϞāĻžāĻŽ āĻāϰ āĻŽāĻ§ā§āϝ⧇ āϰ⧇āϞ⧇āĻļāύāĻļāĻŋāĻĒ established āĻ•āϰ⧇ āϏāĻŦ āĻŸā§‡āĻŦāĻŋāϞ⧇āϰ dublicate āĻ­āĻžāϞ⧁āϗ⧁āϞ⧋āϕ⧇ āĻŽā§āϝāĻžāĻ•ā§āϏāĻŋāĻŽāĻžāĻŽ āĻ•āĻŽ āĻ•āϰ⧇āĨ¤ āĻāχ normalisation āĻĒā§āϰāϏ⧇āϏ āĻāĻ•āĻŽāĻžāĻ¤ā§āϰ āĻ¸ā§āĻŸā§āϰāĻžāĻ•āϚāĻžāϰ āĻĄāĻžāϟāĻžāĻŦ⧇āϏ⧇āϰ āĻ•ā§āώ⧇āĻ¤ā§āϰ⧇āχ āĻĒā§āϰāϝ⧋āĻœā§āϝāĨ¤ Normalisation āĻ•āĻŋāĻ¨ā§āϤ⧁ āĻ•āĻ–āύ⧋āχ 100% āϰāĻžāĻ–āĻž āϏāĻŽā§āĻ­āĻŦ āύāĻžāĨ¤ Normalisation āĻāϰ āĻ•ā§āώ⧇āĻ¤ā§āϰ⧇ 6 āϟāĻŋ āύāĻ°ā§āĻŽāĻžāϞāĻŋāϏ⧇āĻļāύ āφāϛ⧇āĨ¤

  1. 1NF (1st normalisation form)
  2. 2NF
  3. 3NF
  4. BCNF/3.5 NF (boyce-codd normal form)
  5. 5NF
  6. 6NF āĻāĻŦāĻžāϰ āφāĻŽāϰāĻž āύāĻ°ā§āĻŽāĻžāϞāĻžāχāĻœā§‡āĻļāύ āϝāĻ–āύāχ āĻāĻ•āϟāĻž āĻāĻ•āϟāĻž āĻ•āϰ⧇ āϧāĻžāĻĒ⧇ āϧāĻžāĻĒ⧇ āĻāĻ—āĻŋāϝāĻŧ⧇ āϝāĻžāχ āϤāϤāχ āφāĻŽāĻžāĻĻ⧇āϰ āĻĄāĻžāϟāĻžāĻŦ⧇āϏ āϤāϤ āĻŦ⧇āĻļāĻŋ āϐāĻ•ā§āϝāĻŦāĻĻā§āϧ āĻ­āĻžāĻŦ⧇ āĻĨāĻžāϕ⧇āĨ¤ āύāĻ°ā§āĻŽāĻžāϞāĻŋāϏ⧇āĻļāύ⧇āϰ āĻŽāĻžāĻ§ā§āϝāĻŽā§‡ āĻāĻ•āϟāĻž āĻĄāĻžāϟāĻžāĻŦ⧇āϏ⧇āϰ āφāĻ¨ā§āĻĄāĻžāϰ āĻ āĻāĻ•āĻžāϧāĻŋāĻ• āĻŸā§‡āĻŦāĻŋāϞ⧇āϰ āĻŽāĻ§ā§āϝ⧇ āϝāĻĻāĻŋ āĻāĻ•āχ āĻĢāĻŋāĻ˛ā§āĻĄ āĻ•āĻŽāύ āĻĨāĻžāϕ⧇ āϤāĻžāĻšāϞ⧇ āĻŦāĻžāϰ āĻŦāĻžāϰ āφāĻĒāύāĻžāϕ⧇ āϤāĻžāϰ āϜāĻ¨ā§āϝ āĻāĻ•āχ āĻĄāĻžāϟāĻž āχāύāĻĒ⧁āϟ āĻ•āϰāϤ⧇ āĻšāĻŦ⧇āύāĻž, āφāĻŦāĻžāϰ āϝāĻĻāĻŋ āĻ…āύ⧇āĻ• āĻŦāĻĄāĻŧ āϏāĻŋāĻ¸ā§āĻŸā§‡āĻŽā§‡ 2 āϤ⧋ āĻŸā§‡āĻŦāĻŋāϞ⧇āϰ āĻŽāĻ§ā§āϝ⧇ āĻāĻ•āχ āĻĄāĻžāϟāĻž āĻĢāĻŋāĻ˛ā§āĻĄ āĻĨāĻžāĻ•āϞ⧇ 2 āϤ⧋ āĻŸā§‡āĻŦāĻŋāϞ⧇āϰ āϜāĻ¨ā§āϝ āφāϞāĻžāĻĻāĻž āφāϞāĻžāĻĻāĻž āĻĄāĻžāϟāĻž āχāύāĻĒ⧁āϟ āĻ•āϰāĻž āϝāĻžāϝāĻŧāύāĻžāĨ¤ āĻĢāϞ⧇ āĻāĻ•āχ āĻĄāĻžāϟāĻž āĻāĻ•āϟāĻž āĻĢāĻŋāĻ˛ā§āĻĄ āĻāϰ āϜāĻ¨ā§āϝ āϏ⧇āĻ¨ā§āĻŸā§āϰāĻžāϞāĻžāχāϜāĻĄ āĻ­āĻžāĻŦ⧇ āĻĨāĻžāĻ•āϤ⧇ āĻĒāĻžāϰ⧇āĨ¤ āϝāϤ āĻŦ⧇āĻļāĻŋ normalised āĻĢāĻ°ā§āĻŽ āĻāϰ āωāĻĒāϰ⧇āϰ āĻĻāĻŋāϕ⧇ āĻ“āĻ āĻž āϝāĻžāĻŦ⧇ āϤāϤ āĻŦ⧇āĻļāĻŋ āϰāĻŋāĻĄāĻžāύāĻĄā§‡āĻ¨ā§āϏāĻŋ āĻ•āĻŽ āĻšāĻŦ⧇āĨ¤ āύāϰāĻŽāĻžāϞāĻŋ āφāĻŽāĻžāĻĻ⧇āϰ BCNF āĻ…āĻŦā§āĻĻāĻŋ āϗ⧇āϞ⧇āχ āĻ…āύ⧇āĻ•āϟāĻž āĻ āĻŋāĻ• āĻĨāĻžāϕ⧇āĨ¤

18. What are the various forms of Normalization?

Normal Forms are used to eliminate or reduce redundancy in database tables. The different forms are as follows:

Students Table:

StudentAddressBooks IssuedSalutation
SaraAmanora Park Town 94Until the Day I Die (Emily Carpenter), Inception (Christopher Nolan)Ms.
Ansh62nd Sector A-10The Alchemist (Paulo Coelho), Inferno (Dan Brown)Mr.
Sara24th Street Park AvenueBeautiful Bad (Annie Ward), Woman 99 (Greer Macallister)Mrs.
AnshWindsor Street 777Dracula (Bram Stoker)Mr.

Students Table (1st Normal Form)

StudentAddressBooks IssuedSalutation
SaraAmanora Park Town 94Until the Day I Die (Emily Carpenter)Ms.
SaraAmanora Park Town 94Inception (Christopher Nolan)Ms.
Ansh62nd Sector A-10The Alchemist (Paulo Coelho)Mr.
Ansh62nd Sector A-10Inferno (Dan Brown)Mr.
Sara24th Street Park AvenueBeautiful Bad (Annie Ward)Mrs.
Sara24th Street Park AvenueWoman 99 (Greer Macallister)Mrs.
AnshWindsor Street 777Dracula (Bram Stoker)Mr.

Students Table (2nd Normal Form)

Student_IDStudentAddressSalutation
1SaraAmanora Park Town 94Ms.
2Ansh62nd Sector A-10Mr.
3Sara24th Street Park AvenueMrs.
4AnshWindsor Street 777Mr.

Books Table (2nd Normal Form)

Student_IDBook Issued
1Until the Day I Die (Emily Carpenter)
1Inception (Christopher Nolan)
2The Alchemist (Paulo Coelho)
2Inferno (Dan Brown)
3Beautiful Bad (Annie Ward)
3Woman 99 (Greer Macallister)
4Dracula (Bram Stoker)

Students Table (3rd Normal Form)

Student_IDStudentAddressSalutation_ID
1SaraAmanora Park Town 941
2Ansh62nd Sector A-102
3Sara24th Street Park Avenue3
4AnshWindsor Street 7771

Books Table (3rd Normal Form)

Student_IDBook Issued
1Until the Day I Die (Emily Carpenter)
1Inception (Christopher Nolan)
2The Alchemist (Paulo Coelho)
2Inferno (Dan Brown)
3Beautiful Bad (Annie Ward)
3Woman 99 (Greer Macallister)
4Dracula (Bram Stoker)

Salutations Table (3rd Normal Form)

Salutation_IDSalutation
1Ms.
2Mr.
3Mrs.

19. What are Aggregate and Scalar functions?

An aggregate function performs operations on a collection of values to return a single scalar value. Aggregate functions are often used with the GROUP BY and HAVING clauses of the SELECT statement. Following are the widely used SQL aggregate functions:

  • AVG() - Calculates the mean of a collection of values.
  • COUNT() - Counts the total number of records in a specific table or view.
  • MIN() - Calculates the minimum of a collection of values.
  • MAX() - Calculates the maximum of a collection of values.
  • SUM() - Calculates the sum of a collection of values.
  • FIRST() - Fetches the first element in a collection of values.
  • LAST() - Fetches the last element in a collection of values.

Note: All aggregate functions described above ignore NULL values except for the COUNT function.

A scalar function returns a single value based on the input value. Following are the widely used SQL scalar functions:

  • LEN() - Calculates the total length of the given field (column).
  • UCASE() - Converts a collection of string values to uppercase characters.
  • LCASE() - Converts a collection of string values to lowercase characters.
  • MID() - Extracts substrings from a collection of string values in a table.
  • CONCAT() - Concatenates two or more strings.
  • RAND() - Generates a random collection of numbers of a given length.
  • ROUND() - Calculates the round-off integer value for a numeric field (or decimal point values).
  • NOW() - Returns the current date & time.
  • FORMAT() - Sets the format to display a collection of values.

20. What is a Stored Procedure?

A stored procedure is a subroutine available to applications that access a relational database management system (RDBMS). Such procedures are stored in the database data dictionary. The sole disadvantage of stored procedure is that it can be executed nowhere except in the database and occupies more memory in the database server. It also provides a sense of security and functionality as users who can’t access the data directly can be granted access via stored procedures.

DELIMITER $$
CREATE PROCEDURE FetchAllStudents()
BEGIN
    SELECT * FROM myDB.students;
END $$
DELIMITER ;


SQL Query

Interview Question (OSL):

  1. Find out count, name and age from list where age >= 25 AND age ⇐ 26
SELECT COUNT(*) AS Count,
       Name,
       Age
FROM YourTableName
WHERE Age >= 25 AND Age <= 26
GROUP BY Name, Age;

Interview Question (ASD):

  1. You have two tables (EmployeeInfo and Salary). Now joining the two tables and get employee info whose salary is maximum.

1. EmployeeInfo

  • a. Id
  • b. EmployeeId
  • c. EmployeeName
  • d. Designation
  • e. Department
  • f. DateOfJoin

2. Salary

  • a. Id
  • b. EmployeeInfoId (Foreign Key)
  • c. Salary
SELECT e.EmployeeId,
       e.EmployeeName,
       e.Designation,
       e.Department,
       e.DateOfJoin,
       s.Salary
FROM EmployeeInfo e
JOIN Salary s ON e.Id = s.EmployeeInfoId
WHERE s.salary = (SELECT MAX(salary) FROM Salary);

Visit here to get more queries: W3Schools SQL

SQL Statements

  • Create table
CREATE TABLE table_name (col_1 datatype,
                         col_2 datatype,
                         col_3 datatype);
  • Update
UPDATE table_name
SET col_1 = value_1, column_2 = value_2
WHERE condition;
  • Insert
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
  • Delete table
DROP TABLE table_name;

  • Sort
SELECT * FROM table_name
ORDER BY col_1;
 
SELECT * FROM table_name
ORDER BY col_1 DESC, col_3, col_6 DESC;
  • Count
SELECT COUNT(*) FROM Students;
 
SELECT COUNT(Id) FROM Students WHERE Roll BETWEEN 2 AND 5;
 
SELECT Roll, Name
FROM Students
WHERE Roll BETWEEN 5 AND 17;

Others:

  • District
SELECT DISTINCT Name FROM Students;
  • And + Or
SELECT * FROM Students
WHERE Roll BETWEEN 6 AND 10 AND (Name LIKE 'R%' OR Name LIKE 'M%');
  • MAX
SELECT MAX(Roll)
FROM Students;
  • MIN
SELECT MIN(Roll)
FROM Students;
  • SUM(), AVG() as like as MIN(), MAX()

  • Case

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END;
  • ANY
SELECT ProductName
FROM Products
WHERE ProductID = ANY
  (SELECT ProductID
   FROM OrderDetails
   WHERE Quantity > 1000);
  • ALL
SELECT ProductName
FROM Products
WHERE ProductID = ALL
  (SELECT ProductID
   FROM OrderDetails
   WHERE Quantity = 10);

GROUP BY and HAVING

GROUP BY and HAVING are clauses in SQL that are often used together, but they serve different purposes in a query.

1. GROUP BY:

āϧāϰāĻŋ āĻāĻ•āχ āύāĻžāĻŽā§‡āϰ āĻĻ⧇āĻļ⧇ ā§Š-ā§Ē āϜāύ āĻāĻŽā§āĻĒ⧟āĻŋ āφāϛ⧇āĨ¤ āύāĻŋāĻšā§‡āϰ āĻāχ āĻ•ā§ā§Ÿā§‡āϰāĻŋ āĻĒā§āϰāĻŋāĻ¨ā§āϟ āĻ•āϰāϞ⧇ āĻŦāĻžāĻ‚āϞāĻžāĻĻ⧇āĻļ⧇āϰ ā§Š āϜāύ āĻ“ āĻĻ⧇āĻļ⧇āϰ āύāĻžāĻŽ, āĻ­āĻžāϰāϤ⧇āϰ ā§Ŧ āϜāύ āĻ“ āĻĻ⧇āĻļ⧇āϰ āύāĻžāĻŽ āĻāχāĻ­āĻžāĻŦ⧇ āĻ—ā§āϰ⧁āĻĒ āĻ•āϰ⧇ āĻĒā§āϰāĻŋāĻ¨ā§āϟ āĻ•āϰāĻŦ⧇āĨ¤ āϚāĻžāχāϞ⧇ count āĻāϰ āĻŦāĻĻāϞ⧇ Sql function like: COUNT(), MAX(), MIN(), SUM(), AVG() āχāωāϜ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŋ,

  • The GROUP BY clause is used to group rows based on the values in one or more columns.
  • It is typically used with aggregate functions (e.g., COUNT, SUM, AVG) to perform calculations on each group of rows.

Example:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
  • In this example, rows are grouped by the “department” column, and the COUNT(*) function calculates the number of employees in each department.

2. HAVING:

āĻāϟāĻžāĻ“ āĻ—ā§āϰ⧁āĻĒ⧇āϰ āϏāĻžāĻĨ⧇āχ āχāωāϜ āĻšā§Ÿā§‡ āĻ•āĻŋāĻ¨ā§āϤ⧁ āφāϰ⧇āĻ•āϟāĻŋ āĻ•āĻ¨ā§āĻĄāĻŋāĻļāύ āĻĻāĻŋā§Ÿā§‡ āĻĻā§‡ā§Ÿ āϝ⧇ āĻāĻŽāύ āĻ•āĻŋāϛ⧁ āĻĨāĻžāĻ•āϤ⧇ āĻšāĻŦ⧇āĨ¤

  • The HAVING clause is used to filter the results of a GROUP BY query based on a specified condition.
  • It is applied after the GROUP BY and aggregate functions in order to filter groups that meet certain criteria.

Example:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
  • In this example, rows are grouped by the “department” column, and the HAVING clause filters the results to include only those groups where the average salary is greater than 50,000.

Summary:

In summary, GROUP BY is used to group rows based on certain columns, and aggregate functions are often applied to these groups. The HAVING clause is then used to filter the grouped results based on conditions involving the aggregated values.


Store Procedure

What is a Stored Procedure?

A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again. So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it. You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter value(s) that is passed.

Store procedure: (Single parameter)

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30)
AS
SELECT * FROM Customers WHERE City = @City
GO;
 
EXEC SelectAllCustomers @City = 'London';

Store procedure: (Multiple parameter)

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode = @PostalCode
GO;
 
EXEC SelectAllCustomers @City = 'London', @PostalCode = 'WA1 1DP';

SQL Cursor

Visit here to get advanced details: What is Cursor in SQL

While loop using cursor:

DECLARE @id INT = 1;
 
WHILE @id <= (SELECT MAX(id) FROM employees)
BEGIN
    UPDATE employees
    SET salary = salary * 1.1
    WHERE id = @id;
 
    SET @id = @id + 1;
END;