SQL VCTC Student Notes
SQL VCTC Student Notes
SQL Introduction
SQL- Structured Query Language
RDBMS-
➢RDBMS stands for Relational Database Management System.
➢ RDBMS is the basis for SQL, and for all modern database systems
such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft
Access.
3
Creating DB, Creating table, updating data in table, Delete
4
5
Few Popular Databases Management Studio
6
SQL Command Types
➢ DDL(Data Definition Language): - Allows to work with the Structure or
Definition of the database or tables
•SQL Commands come under DML are as follows: INSERT, UPDATE, DELETE,
SELECT
➢ DQL(Data Query Language): - Deals with the data but to retrieve thedata
8
Table name Attribute names/ header
Tables in SQL
Product
Sr no./
Price Category Manufacturer
PName
Gizmo $50 Gadgets GizmoWorks
Tuples Row
9
Data Types in SQL
Characters / String:
CHAR(20) -- fixed length
VARCHAR(255) -- variable length
Numbers:
INT
REAL, FLOAT (10%) -- differ in precision
Decimal (4.9403)
Times and dates:
DATE
DATETIME -- SQL Server
Binary data type
Binary
10
SQL operators
Type Operator Description Example
11
SQL operators
Type Oper Description Example
ator
<> Checks, values of two operands are equal or not (10 <> 4) is true.
Checks, value of left operand with greater/lesser (10 > 35) is not
>, < value with right operand true.
Comparison
Operators >=, Checks, value of left operand with greater/lesser (a >= b) is not
<= than or equal to the value of right operand true.
❑ Alter - The ALTER TABLE statement is used to add, drop, or alter columns in
an existing table.
16
Tables in SQL
Fname Lname Gender Mockresult Location
VCTC
Aditya Patki M 7 Nagpur
19
SQL DML command-
❑ DISTINCT clause/keyword is used in conjunction with the SELECT statement
to eliminate all the duplicate records and fetching only unique records.
❑ TOP clause is used to fetch a TOP N number or X percent records from a table.
20
SQL DML command-
❑ Aggregate Function- Aggregate used with select statements, they will
return some numeric values
22
SQL DML command-
❑ AND, OR, NOT- operators are used to combine multiple conditions to narrow data
in an SQL statement.
26
SQL DML command-
❑ Order By- ORDER BY clause is used to sort the records in your result set
❑ Alise - COLUMN ALIASES are used to make changes column headings in your
result set easier to read & same as TABLE ALIASES are used to shorten your SQL to
make it easier to read.
❑ Union All - UNION ALL operator is used to combine the result sets of 2 or more
SELECT statements. It returns all rows from the query and it does not remove
duplicate rows between the various SELECT statements.
Each SELECT statement within the SQL Server UNION ALL operator must have the
same number of fields in the result sets with similar data types.
29
SQL DML command-
❑ SELECT INTO- SELECT INTO statement is used to create a table from an
existing table by copying the existing table's columns.
Ex. SELECT *
INTO VCTCViman
FROM VCTC;
30
SQL DML command-
Question –
31
Simple SQL Query
Product PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
32
33
SQL Table constrains-
❑ SQL constraints are used to specify rules for data in a table. Constraints can be
used when the table is created with the CREATE TABLE statement
❑ NOT NULL - The NOT NULL constraint forces a column to NOT accept NULL
values. Becz while creating table if value not present it take NULL value in C.N.
❑ UNIQUE - The UNIQUE constraint ensures, all values in a column are different.
❑ PRIMARY KEY - The PRIMARY KEY constraint uniquely identifies each record
in a table. Primary keys must contain UNIQUE values, and cannot contain NULL
values.
❑ FOREIGN KEY - A FOREIGN KEY is a key used to link two tables together. A
FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.
❑ CHECK - The CHECK constraint is used to limit the value range that can be
placed in a column. If you define a CHECK constraint on a single column it allows only
certain values for this column.
❑ DEFAULT - The DEFAULT constraint is used to provide a default value for a
column.
34
Tables in SQL
Fname Lname Gender Mockresult Location
VCTC
Aditya Patki M 7 Nagpur
36
Constrains Difference-
Primary Key Foreign Key Unique Key
The PRIMARY KEY A FOREIGN KEY is a key The UNIQUE constraint
constraint uniquely used to link two tables ensures that all values in a
identifies each record in a together. A FOREIGN KEY is column are different.
table. Primary keys must a field (or collection of
contain UNIQUE values fields) in one table that
refers to the PRIMARY KEY
in another table.
Primary key cannot have a Foreign key can accept Unique Constraint may have
NULL value. multiple null value. a NULL value.
Each table can have only one We can have more than one Each table can have more
primary key. foreign key in a table. than one Unique Constraint.
Primary key is clustered Foreign keys do not Unique key is a unique non-
index automatically create an clustered index
index, clustered or non-
clustered
37
SQL Join-
❑ JOINS are used to retrieve data from multiple tables. A SQL Server JOIN is
performed whenever two or more tables are joined in a SQL statement.
39
SQL Join-
❑ FULL JOIN - This type of join ❑ SELF JOIN - A self join is a join in
returns all rows from the LEFT- which a table is joined with itself
hand table and RIGHT-hand table (which is also called Unary
with nulls in place where the join relationships). The self join can be
condition is not met. viewed as a join of two copies of the
same table. The table is not actually
copied, but SQL performs the command
as though it were.
❑ Index –Indexes are used to retrieve data from the database more quickly than
otherwise. The users cannot see the indexes, they are just used to speed up
searches/queries.
Syntax- CREATE INDEX index_name
ON T.N. (C.N);
42
Ex- CREATE INDEX Vctclist
ON VCTC (StudentID);
SQL Questions-
1. Explain DML and DDL?
2. How many Aggregate functions are available in SQL?
3. What is the difference in BETWEEN and IN condition operators?
4. What is the difference between the HAVING clause and WHERE
clause?
5. What is the difference between DELETE, TRUNCATE & DROP ?
6. What are different Clauses used in SQL?
7. What are different SQL constraints?
8. What is the difference between UNIQUE key, PRIMARY KEY &
FORGIN KEY constraints?
9. What are different JOINS used in SQL?
43
SQL Questions-
10. How to write a query to show the details of a student from Students
table whose name start with K?
11.What is the syntax to add a record to a table?
12. What is the syntax of GROUP BY in SQL?
13. Define the SQL DELETE statement.
14. Write a SQL SELECT query that only returns each name only once
from a table?
15. Write an SQL query to get the first maximum salary of an employee
from a table named employee_table.
16. Write an SQL query to get the second maximum salary of an employee
from a table named employee_table. 44
17. Write an SQL query to get the third maximum salary of an employee
from a table named employee_table.
SQL Questions-
18. Write an SQL query to fetch unique values from a table?
19. Write an SQL query to fetch data from table whose name start with
Vipul & Krishna?
20 Write an SQL query to print details of the Workers whose SALARY lies
between 100000 and 500000.
21. Write an SQL query to print details of the Workers who have joined in
Feb’2014.
22. Write an SQL query to fetch the count of employees working in the
department ‘Admin’.
23. Write an SQL query to fetch worker names with salaries >= 50000 and
<= 100000.
https://www.techbeamers.com/sql-query-questions-answers-for-practice/
46