0% found this document useful (0 votes)
3 views

SelfMadeSQL

The document provides a comprehensive overview of databases, database management systems (DBMS), and relational database management systems (RDBMS), including their definitions, types, and key concepts such as SQL, tables, keys, and constraints. It explains various SQL commands, joins, and data integrity, along with aggregate functions and clauses used in SQL queries. Additionally, it covers the creation and manipulation of databases and tables, as well as the importance of data types and indexing.

Uploaded by

Sanju Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SelfMadeSQL

The document provides a comprehensive overview of databases, database management systems (DBMS), and relational database management systems (RDBMS), including their definitions, types, and key concepts such as SQL, tables, keys, and constraints. It explains various SQL commands, joins, and data integrity, along with aggregate functions and clauses used in SQL queries. Additionally, it covers the creation and manipulation of databases and tables, as well as the importance of data types and indexing.

Uploaded by

Sanju Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

1. What is Database?

Any collection of related information.


1.Phone Book
2.Shopping List
3.Todo list
4.Facebook User data

2. What is DBMS?
A special software program that helps users to create and maintain database.
 Makes it easy to manage large amount of information.
 Handles security
 Backups
 Importing/exporting data
 Concurrency
 Interacts with software applications

Two Types of database


1.Relational Database
 Organize data into one or more table.
 Each table has coloumns.
 A unique key identifies each row.

2.Non-Relational Database (noSQL/ not just Sql)


 Any type of database that is not relational database.
 Organize data in anything but a traditional table.
 Key-value Stores
 Document (JSON/XML)
 Graphs

Relational Database
Relational Database Management System(RDBMS)
Helps user to create and maintain a relational database.
 MySQL
 Oracle
 PostreSQL etc

2. What is RDBMS? How is it different from DBMS?


RDBMS is used to store or manage only the data that are in the form of tables.
whereas DBMS stores data in the form of files.
RDBMS stands for Relational Database Management System.

4. What is the difference between SQL and MySQL?


SQL is a standard language for retrieving and manipulating relational databases. On the contrary,
MySQL is a relational database management system software, like SQL Server, Oracle or IBM DB2,
that is used to manage SQL databases.

5. What is SQL?
SQL is a language used for interacting with relational database management
system(RDBMS).
 You can use SQL to get the RDBMS to do things for you.
 Create, delete, update data.
 Design and create database tables.
 Perform administration tasks.

6. What are Tables and Fields?


A table is an organized collection of data stored in the form of rows and columns.

The columns in a table are called fields.

the rows can be referred to as records.

Q7. What are keys in sql?


keys are attributes or combinations of attributes that uniquely identify rows or
records within a table.

Q8. What is a Primary Key?


A primary key is basically an attribute (Column) which uniquely defines the row
in the database.
A table in SQL is strictly restricted to have one and only one primary key, which
is comprised of single or multiple fields (columns).

Primary Key= { Unique + Not Null }


CREATE TABLE Students ( /* Create table with a single field as primary key */
ID INT NOT NULL
Name VARCHAR(255)
PRIMARY KEY (ID)
);

CREATE TABLE Students ( /* Create table with multiple fields as primary key */
ID INT NOT NULL
LastName VARCHAR(255)
FirstName VARCHAR(255) NOT NULL,
CONSTRAINT PK_Student
PRIMARY KEY (ID, FirstName)
);

ALTER TABLE Students /* Set a column as primary key */


ADD PRIMARY KEY (ID);
ALTER TABLE Students /* Set multiple columns as primary key */
ADD CONSTRAINT PK_Student /*Naming a Primary Key*/
PRIMARY KEY (ID, FirstName);

Q9. What is a Foreign Key?


 It is an attribute or set of attribute that references to primary key of the
same table or some another table.
It maintains the referential intergrity.

First table: Student table

S_Id= Primary key

S_Id LastName FirstName CITY

1 MAURYA AJEET ALLAHABAD

2 JAISWAL RATAN GHAZIABAD

3 ARORA SAUMYA MODINAGAR

Second table: Orders Table

S_ID= foreign key

O_Id OrderNo S_Id

1 99586465 2

2 78466588 2

3 22354846 3

4 57698656 1
Q10.What is Super Key?
A superkey is a combination of columns that uniquely identifies any row within a
relational database management system (RDBMS) table.

Q11. What is Candidate key?


A candidate key is a set of one or more columns that can uniquely identify rows
in a table. From these candidate.
Ab issi ke set me se hm select karte hai primary key.
Or baaki ki saari bachi huyi ban jaati hai alternate key

Composite Key: A composite key, also known as a compound key, is a key that
consists of two or more columns to uniquely identify a row in a table. It's useful
when a single column doesn't provide enough uniqueness.

Unique Key: Similar to a primary key, a unique key constraint ensures that all
values in the key column(s) are unique, but unlike a primary key, it allows for null
values (except in SQL Server). A table can have multiple unique keys.

Q12. What are the data type available in database?


For every database, data types are primarily classified into three
categories.
 Numeric Datatypes
 Date and Time Database
 String Database

1.INT------- Whole number


2. Decimal-------------- Decimal Number -Exact values
( M= total number of didgits,
N = Number of digits after decimal )

3.Varchar---- String of text length 1.


4.Blob-----------Binary large objects stores large data
5. Date----- yyyy-mm-dd
6.TimeSpan------------YYYY-MM-DD
Q13. What are the main SQL commands ?
1. DDL – Data Definition Language
2. DQL – Data Query Language
3. DML – Data Manipulation Language
4. DCL – Data Control Language
5. TCL – Transaction Control Language

Q14. What is a Query?


A query is a set of instruction given to the RDBMS ( written is sql) to tell the RDBMS what
information you want it to retrieve for you.
 TONS of data in a database
 Offen hidden in a complex schema
 Goal is to only get the data you need

SELECT employee.name, employee.age from employee WHERE employee.salary>30000

Q15. How to create database and a table?


Q16. Inserting data in table .

Suppose we want to store the data and we don’t have the one column value major

Q17. 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
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.

Q18. What is update statement in sql?


An SQL UPDATE statement is a database operation used to modify existing
records within a table. It allows you to change the values of one or more columns
in specific rows based on certain conditions.


Q20. What is a Join? List its different types.
In SQL, joins are used to combine rows from two or more tables based on
a related column between them. Joins allow you to retrieve data from
multiple tables.

1. inner join,
2. left outer join,
3. right outer join,
4. full outer join, and
5. cross join.
1. INNER JOIN: Returns only the rows that have matching values
in both tables based on the specified join condition.

Columns name should be same.


Do table ke beech jo bhi related columns hai.
2. LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table
(first table mentioned in the query) and the matched rows from the right
table. If there is no matching row in the right table, NULL values are
returned.
Example:

Who saara data milta hai, jo left table me hai or woh agar match karta hai
right table se………Jo table pehele likhenege who left table ban jaayegi or
dusri right table
FULL JOIN = A FULL JOIN in SQL is a type of join operation that
combines the results of both a LEFT JOIN and a RIGHT JOIN. It returns all
records when there is a match in either the left or right table. If there is no
match, NULL values are returned for the columns from the table that lacks a
matching row.
12. What is a Self-Join?
Jb ap same table me, different columns ko join karte ho.
Ek hi table ke name ko 2 baar likhte hain

Q. What is cross join?


a cross join is a type of join operation that combines each row from one table
with every row from another table, resulting in a Cartesian product. Unlike other
join types, such as inner joins or outer joins, cross joins do not require any
matching condition to establish relationships between the tables.

13. What are UNION, MINUS and INTERSECT commands?


Dono me same coloum hai but ek row repeat huya hai data.
Union All me duplicated bhi milenge

MINUS (or EXCEPT): The MINUS operator, often called EXCEPT in some
database systems, is used to retrieve the rows from the first SELECT
statement that are not present in the result set of the second SELECT
statement. It is similar to the set difference operation in mathematics.
Example:
INTERSECT: The INTERSECT operator is used to retrieve the rows that
appear in both result sets of the SELECT statements. It returns only
distinct rows that are common to both SELECT statements.
Example:
14. What is an Index? Explain its different types.
A database index is a data structure that provides a quick lookup of data in a column or columns of a
table. It enhances the speed of operations accessing data from a database table at the cost of
additional writes and memory to maintain the index data structure.

3.because joins ke sath jo data milta hai kaafi lengthy hota hai toh isliye mostly joins ke liye
hm index laga dete hain.
Index create karne ke bad time kam hogya upper wali query ka.

Cluster index = arranges data in order wise ( fast operation happens ).

16. What is Data Integrity?


Whatever data we store should be true and honest.
Data Integrity is the assurance of accuracy and consistency of data over its entire lifecycle and is a
critical aspect of the design, implementation, and usage of any system which stores, processes, or
retrieves data. It also defines integrity constraints to enforce business rules on the data when it is
entered into an application or a database.

17. What is a Query?


A query is a request for data or information from a database table or combination of tables. A
database query can be either a select query or an action query.
18. What is a Subquery? What are its types?
A subquery is a query within another query, also known as a nested query or inner query. It is used
to restrict or enhance the data to be queried by the main query, thus restricting or enhancing the
output of the main query respectively. For example, here we fetch the contact information for
students who have enrolled for the maths subject:

There are two types of subquery - Correlated and Non-Correlated

19. What is the SELECT statement?


SELECT operator in SQL is used to select data from a database. The data returned is stored in a result
table, called the result-set.

21.What are Aggregate Functions?


MySQL's aggregate function is used to perform calculations on multiple
values and return the result in a single value like the average of all
values, the sum of all values, and maximum & minimum value among certain
groups of values. We mostly use the aggregate functions with SELECT
statements in the data query languages.
1.Count 
COUNT(*) Function: This function uses the SELECT statement to returns
the count of rows in a result set. The result set contains all Non-Null, Null,
and duplicates rows.

COUNT(expression) Function: This function returns the result set


without containing Null rows as the result of an expression.

COUNT(distinct expression) Function: This function returns the count


of distinct rows without containing NULL values as the result of the
expression.
1.

OUTPUT 8

2.

OUTPUT5

3.

OUTPUT4

2.Sum()
The MySQL sum() function is used to return the total summed value of an
expression. It returns NULL if the result set does not have any rows.
3.MySQL avg() function

21. What are SQL Clauses?


Clauses are specific keywords or commands used within SQL statements to
perform various operations on databases.

1.WHERE 
It filters the records. It returns only those queries which fulfill the specific
conditions..

WHERE clause is used in SELECT, UPDATE, DELETE statement etc.

SELECT column1, column 2, ... column n


FROM table_name
WHERE [conditions]
WHERE clause uses some conditional selection

= equal

> greater than

< less than

>= greater than or equal

<= less than or equal

<> not equal to


2.ORDER BY
Whenever we want to sort the records based on the columns stored in the
tables.
The ORDER BY clause in SQL will help us to sort the records based on the
specific column of a table. This means that all the values stored in the column on
which we are applying ORDER BY clause will be sorted.
Using the ORDER BY clause, we can sort the records in ascending or
descending order as per our requirement. ASC or DESC

3. 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.

Jese ki employee table me employee ko group karna hai based on department.


4. DISTINCTDISTINCT clause is used to remove duplicate records from
the table and fetch only the unique records. The DISTINCT clause is only used
with the SELECT statement.

5. MySQL FROM Clause


The MySQL FROM Clause is used to select some records from a table. It can
also be used to retrieve records from multiple tables using JOIN condition.

6.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.

RESULT= IT
Group by ke sath WHERE clause kaam nhi karta bcz WHERE Clause puri table ke liye kaam
karta hai ….or humne GROUP BY laga kar table ko groups me divide kar diya hai.
Also print the name of the employee

Result = varun

Q. What is view in SQL.


It is a virtual table.
It looks like a table but it is not a actual table.
View is a stored Query. It is result set of stored query.
Readable vs Updatable  agar base table me kuch bhi change karte ho toh view me bhi
change hoga.
Materialized View  to keep copy of data which is stored in server.

1. Creating view
2. Creating View from a single table

OUTPUT

3. Creating View from multiple tables


4. Deleting View
SQL Queries

3.
NAME 
In isliye use kiya becoz hm kisi ek particular row se compare nhi kar rahe hai,

=  tb use hota hai jb ek value se compare hota hai

IN/NOT IN
Jb bhi hmre pass comparison ke liye ek taraf 2 ya 2 se Jayada values toh IN use karte hai.
EXIST/ NOT EXIST

You might also like