SQL Crash Course PDF
SQL Crash Course PDF
SQL Crash Course PDF
Contents:
1. Introduction to Databases
2. Database Management System
3. Normalization of Database
4. SQL
5. Data Definition Language
6. Data Manipulation Language
7. Data Query Language
8. Table Constraints
9. Where, Comparison Operators, Logical Operators
10. Group By, Having, Limit, Order By
11. Joins
12. Nested Queries
13. Case When Statements, If Else Statements
Introduction to Databases:
A database is a collection of related data items, which are
linked and structured so that the data can be accessed in a
number of ways.
ACID Properties:
A database's contents can be accessed and possibly modified as part of a
single logical unit of work known as a transaction. Read and write
operations are used by transactions to access data.
Certain attributes are followed before and after the transaction in order to
preserve consistency in a database. We refer to these as ACID
characteristics.
Advantages of DBMS
o Controls database redundancy
o Data sharing
o Easily Maintenance
o Reduce time
o Backup
o multiple user interface
Disadvantages of DBMS
o Cost of Hardware and Software
o Size
o Complexity
o Higher impact of failure
Normalization of Database:
Normalization is the process of organizing the data in the database.
Normalization is used to minimize the redundancy from a relation or set of
relations. It is also used to eliminate undesirable characteristics like
Insertion, Update, and Deletion Anomalies.
Normalization divides the larger table into smaller and links them using
relationships.
The normal form is used to reduce redundancy from the database table.
o 3NF is used to reduce the data duplication. It is also used to achieve the data
integrity.
1. Create command:
Used to create a database or a table.
Syntax:
2. Alter command:
An alter command modifies an existing database table.
Syntax:
3. Drop command:
A drop command is used to delete objects such as a table, index
or view.
Syntax:
Q3. Drop the column ‘rate’ and then the entire table.
4. Truncate command:
Similar to DROP, the TRUNCATE statement is used to quickly
remove all records from a table.
Syntax:
1. Insert command:
Used to insert records in a database.
Syntax:
2. Update command:
Command to change or update current/existing data.
Syntax:
3. Delete command:
Syntax:
DELETE FROM table_name WHERE condition;
1. Select command:
NOT NULL: This constraint tells that we cannot store a null value in a
column. That is, if a column is specified as NOT NULL then we will not be
able to store null in this particular column any more.
UNIQUE: This constraint when specified with a column, tells that all the
values in the column must be unique.
PRIMARY KEY: A primary key is a field which can uniquely identify each
row in a table.
FOREIGN KEY: A Foreign key is a field which can uniquely identify each
row in another table.
DEFAULT: This constraint specifies a default value for the column when no
value is specified by the user.
Where, Comparison Operators, Logical
Operators
Comparison Operators:
Logical Operators:
Q9. Return the records of cars of company ‘Maruti Suzuki’.
Q10. Retrieve the car names having mileage greater than 75.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s);
Q14. Find the number of cars of each type.
SELECT column(s)
FROM table_name
WHERE [condition]
GROUP BY column_names
HAVING [condition]
ORDER BY column_names;
SQL Join statement is used to combine data or rows from two or more
tables based on a common field between them. Different types of Joins
are as follows:
A nested query is a query that has another query embedded within it.
The embedded query is called a subquery.
Q23. Display the model number, model name of the cars that
have been bought by the buyers.
Case When Statements, If Else Statements
The case statement in SQL returns a value on a specified condition. We
can use a Case statement in select queries along with Where, Order By,
and Group By clause. It can be used in the Insert statement as well.
Syntax:
SELECT CASE Expression
When expression1 Then Result1
When expression2 Then Result2
...
ELSE Result
END
Q24. Retrieve the model name and corresponding launch
year in the form:
Syntax:
IF(condition,result1,result2);