You are on page 1of 15

1. What is SQL?

Structured Query Language (SQL) is a language used to access and manipulate data on relational
databases.

2. Why SQL Important?


SQL is important for managing big data, allowing users to easily search, sort, and analyze large
datasets.

3. What are the SQL Commands?


SQL commands are used to interact with databases and help users perform various operations
such as creating databases or tables, inserting data, and retrieving data.

 DDL: Data Definition Language - Used to define database structure.


 DQL: Data Query Language - Used to retrieve data from databases or tables.
 DML: Data Manipulation Language - Used to Modify the data that present in the SQL
table.
 DCL: Data Control Language - Used to control the access the data.
 TCL: Transaction Control Language – Used to manage and control the transactions.

4. DDL: Data Definition Language - Used to define database structure.


 CREATE: Used to create a new table or database.
 ALTER: Used to modify the structure of an existing table.
 RENAME: Used to change the name of an existing table or column.
 TRUNCATE: Used to delete all data from a table.
 DROP: Used to delete an entire table or database.

5. DQL: Data Query Language - Used to retrieve data from databases or tables.
 SELECT: Used to retrieve or return the required data from a table.
 FROM: Used to select the table from which data is obtained.
 ORDER BY: Used to sort the data retrieved by a certain column.
 GROUP BY: Used to group retrieved data by a specific column. Aggregates the data.
 HAVING: Used to filter the aggregated data based on a specified condition.

6. DML: Data Manipulation Language - Used to Modify the data that present in the SQL table.
 Insert: Used to add new rows of data to a table.
 Delete: Used to remove or delete one or more rows of data from a table.
 Update: Used to modify existing data in a table.

7. DCL: Data Control Language - Used to control the access the data.
 GRANT: Used to give user access to a database.
 REVOKE: Used to take away user access to a database.

8. TCL: Transaction Control Language – Used to manage and control the transactions.
 COMMIT: Used to save all changes made within a transaction.
 ROLLBACK: Used to revert or undo all changes made within a transaction.
 SAVE POINT: Used to set a specific point in a transaction to which changes can be rolled
back.

9. What is MySQL?
It is freely available open-source RDBMS (Relational Database Management System) that uses
SQL. Information is stored in the form of table in MySQL Database. A single MySQL database can
contain many tables at once and stored thousands of records.

10. What is Importance of MySQL?


 It provides strong data security.
 It gives high performance.
 Saves the cost as it is free.
 On demand scalability

11. What is difference between SQL Language and MySQL Programming Language?
SQL stands for Structured Query Language. It's a special language used to communicate with
databases. Think of it as a way to talk to a database and ask it for information.
Imagine you have a big collection of data, like a list of books in a library. If you wanted to find all
the books by a certain author, you could ask using SQL. It helps you retrieve, update, and manage
data in a database.
MySQL, on the other hand, is a specific program that uses SQL. It's like a tool that understands
and executes the instructions you give in SQL.
Now, let's break down some basic SQL concepts:
1. Tables 2. Queries 3. Select 4. Insert 5. Update 6. Delete

12. What is the difference between SQL and MySQL?


SQL is a Structured Query Language that is used for access and manipulating data on relation
database.

MySQL itself relational database that uses SQL as the standard database language.

13. What is the difference between SQL and NOSQL?


SQL is a Structured Query Language that is used for access and manipulating data on relation
database.

NoSQL databases are nontabular databases that store data in JSON documents instead of
relational databases.

14. What is the difference between SQL and PL/SQL?


SQL is a Structured Query Language that is used for access and manipulating data on relation
database whereas PL/SQL comes with procedural concepts of programming languages.

15. What is Data?


Data is described as fact, numbers and other forms of information that are generally stored and it
can be smaller volume. Data can be several forms, including text, number and bits and bytes.

16. What is Database?


Database is collection of data that can stored in computer so that it can be accessed, managed,
and updated easily.

Databases are often created using formal design and Modeling approaches, and they are usually
managed using a Database Management System (DBMS).

17. Types Of Databases?


 Relational Database
 NoSQL Database
 Graph Database
 Centralized and Distributed Database

18. What is Database Management System (DBMS)?


A database management system (DBMS) is a software that stores and retrieves data based on the
user’s requirements. DBMS acts as an interface between the data and software.

19. What is Relational Database Management System (RDBMS)?


A relational database management system (RDBMS) is an advanced version of a database
management system (DBMS). It is the most popular DBMS in the market.

Few examples of RDBMS are MySQL, Oracle, and Microsoft SQL Server.

20. What is difference between DBMS and RDBMS?

DBMS RDBMS
It stores data as a file. It stores data in the form of tables.
It only supports a single user. It supports multiple users.
It stores data which are not related to each Data stored in tables are related via foreign
other. keys.
Data fetching is slower for the complex and Data fetching is rapid because of the
large volumes of data. relational approach.
It has no security. It has multiple levels of security.

Examples: File System, XML, MS Access Examples: MySQL, PostgreSQL, Oracle, and
SQL Server

21. What is the Database Manipulation commands in MySQL?


The followings are the most important commands for Databases in the MYSQL database.

 Create Database - Create new database in MySQL.


 Show Database - To see or list all databases which were created in MYSQL.
 Use – Use this command for interacting database in MySQL.
 Drop Database – Delete the existing databases permanently from MySQL.

22. What is Data Type?


Data types refer to the nature or format of the data that can be entered into a database.

Data types are divided into three categories.

I. String
II. Numeric
III. Date and Time

23. What are the tables in MySQL?


Tables are in database with unique names which consist of a collection data stored in a database.
 A Table is collection of number of rows and columns.

24. What are the table manipulation commands in MySQL?


The followings are the most important commands for tables in the MYSQL database.
 CREATE TABLE - Creating new table in the database.
 SHOW TABLES - To see or list all TABLES which were created in MySQL.
 DESCRIBE - view the table structure of the table.
 DROP TABLE - delete existing table permanently from the MYSQL database.
 Insert Into – Used to add new rows data to table.
 TRUNCATE TABLE - delete all data in the existing table.
 ALTER TABLE - Used to modify the structure of an existing table and
perform below actions.
(a) Adding columns to a Table.
(b) Modify columns to a table.
(c) Renaming columns in a table.
(d) Dropping columns in a table.
(e) Renaming Table.

25. What are the table Rows manipulation commands in MySQL?


Update: - Used to modify existing data in a table. update is the command which is used to update
values at place. Here “SET” is used to setting the new values and “WHERE” Clause used to refer
old balance.

Delete: - Used to remove one or more rows of data from a table.

26. What are the different clauses used in SQL?


 Select: Returns or retrieve the required data
 From: Specifies or select the table from which the data is obtained or retrieved.
 Where: Applies the condition to filter data
 Order By: Used to sort the data retrieved by a certain column in ASC/DESC Order.
 Group By: Used to group retrieved data by a specific column. It Aggregate the data.
 Having: Used to filter data based on a specified condition. Filter the aggregated data.
 Join: Derives common data from a different table.

27. What is the Order of Execution in the SQL?


Below one the order of execution in the SQL. We must explain in the below order.
 From: Specifies or select the table from which the data is obtained or retrieved.
 Join: Derives common data from a different table.
 Where: Applies the condition to filter data
 Group By: Used to group retrieved data by a specific column. It Aggregate the data.
 Having: Used to filter data based on a specified condition. Filter the aggregated data.
 Select: Returns or retrieve the required data.

28. How to select all records from the table?


To select all records from the table, follow the below syntax:
Select * from table_name;

29. What is difference between Truncate, Delete and Drop?


The basic difference between this Truncate command is DDL command, delete command is DML
command, drop command is DDL Command.

DELETE command is used to delete a specific row from the table whereas the TRUNCATE
command is used to remove all rows from the table.

We can use the DELETE command with the WHERE clause but cannot use the TRUNCATE
command with it.

TRUNCATE removes all rows from the table which cannot be retrieved back.
DROP removes the entire table from the database, and it also cannot be retrieved back.
30. What is Relationship in MySQL?
A relationship is a condition that exists between two tables in a database when data from one
table reflects data from another table.
 Two tables are required to form a table relationship at the same time, one of which is
called the primary or parent table and the other the related or child table.

31. What are Views in MySQL?


Views are virtual table in SQL that are created by selecting fields from one or more tables present
in the database.

32. What is difference between table and View?

Table View
1. Tables are database objects with 1. Views are virtual tables in SQL that
unique names which consist of a are created by selecting fields from
collection of data stored in a one or
database. more tables present in the database
2. Tables contains data and exist 2. Views do not contain any data and do
physically in the database. not exist physically in the database
3. A table is an independent data object. 3. Views depends on tables.

33. What is CTE?


CTE or Common Table Expression contains temporary results set which is defined in SQL
Statements.

34. What is the difference between Local and Global temporary tables?
Local temporary table exists only for the duration of that statements. Global temporary table
exists permanently in the database, but its rows disappear when the connection is closed.

35. What is the difference between the HAVING clause and WHERE clause?
Both specify search condition but Having clause is used only with select statement but usually
used with Group By Clause. If the Group By Clause is not used, then Having clause behave like a
where clause.
Data Manipulation in My SQL
The following are the most important SQL commands for database manipulation.

 CREATE DATABASE
 SHOW DATABASE
 USE
 DROP DATABASE

 Creating database in My SQL :

To build a new Mysql database, use the CREATE DATABASE.

Syntax : CREATE DATABASE database_name;

 Listing available database in My SQL :

To lists all database in the Mysql database, use the SHOW DATABASE statement.

Syntax : SHOW DATABASE;

 Deleting Database in My SQL :

The DROP DATABASE statement is used to delete all tables in an existing database and permanently delete that
database.

Syntax : DROP DATABASE database_name;

 Database selection in My SQL :

 It is necessary to notify mysql about the destination database before interacting with it.
 To select an existing database from the mysql database use the USE Statement.
 To View the existing selected Database from Mysql database, we use the SELECT DATABASE() Statement.

Syntax for selecting a default database

Syntax : USE database_name;

Syntax for getting the name of the default database

Syntax : select Database () ;

Data Types
Data type refer to the nature or format of data that is entered into a columns of the table. In the simple words, the
data type reflects the kind of information stored in the field.

We have three categories of data types :


 String
 Numeric
 Date and time

String : It contains letters, numbers, and special characters ( Ex : TN2315@ )

Numeric : It has only a number or a number with decimal places ( Ex : 120130 or 230.15 )
Date and Time: It contains date and time ( Ex : 120130, 23:10:00, 20210526 23:10:00 )

 STRING DATA TYPES IN SQL :

 VARCHAR : variable length character string. ( Ex : ‘Hello’ (0-65535) )


 CHAR : A fixed length character string. ( Ex : ‘John’ (0-255) )
 TEXT : A large text value. ( Ex : 0-65535 )

 NUMERIC DATA TYPES IN SQL :

 NUMERIC : Only a number or a number with decimal places. ( Ex : 3456, or 3.1415 )


 INT : A whole number without decimal places ( Ex : 42 )
 FLOATING : A number with decimal places( Ex : 3.1415 )
 DOUBLE : A whole number without decimal places. ( Ex : 42 it hold more than 6 digits )

 Data and Time Data Types in SQL :

 Date and time : Contains Date and Time ( Ex : 20210526,23:10:00, 20210526 23:10:00)
 Date : A data Value ( Ex : 2023-04-12 ; YYYY-MM-DD)
 Date time : A date and Time value (Ex : 2023-04-12 14:30:00)
 Time : Time Value (Ex: ’14:30:00’)
 Timestamp : Including both the date and the time down to fractions of a second.

Table manipulation commands in MYSQL

Following are the most imp SQL Commands for the table manipulation.
1. Create Table
2. Describe
3. Show Tables
4. Drop Table
5. Inserting Data in a Table
6. Querying Data from a Table
7. Truncate Table
8. Alter Table

 Creating table in mysql :

To build a new Mysql table in a specific database use the CREATE TABLE statement.

Syntax :

CREATE TABLE table-name

column_1_definition,

column_2_definition,

);

Example : create table veena ( Emp_ID int, Emp_name varchar (50), Gender varchar(1));

 Analyzing Table Description in Mysql :


To view the structure of a mysql table use the DESCRIBE statement.

Syntax : DESCRIBE Table-name;

Example: describe veena;


 Listing Existing Tables in mysql :
To List all tables in the Mysql Database use the SHOW TABLES Statement.

Syntax : SHOW TABLES;

 Dropping tables in mysql :


To remove existing tables in the mysql database, use the DROP TABLE statement.

Syntax : DROP TABLE (IF EXISTS) table_name;

Example : drop table if exists Veena;

 Inserting data in Tables :

In mysql the INSERT statement is used to insert one or more rows to a table.
The number of columns and values must be the same when using the INSERT statement. Furthermore, the columns
positions must correspond to the positions of their values.

Syntax: INSERT INTO table_name(c1, c2,…..cn) values (v1, v2, …….vn);

Example : insert into veena(emp_id, emp_name, gender) values (1,’Xman’,’M’), (2,’Veena’,’F’);

 Querying data from Tables :

To query data from one or more tables, use the SELECT statement.

Syntax : SELECT select_list FROM table_name;

Parameters:

Keywords Meaning
Select-List Unique names of one or one target columns separated by a comma , to view their data
Table name A unique name of the target table having the required columns.

 Truncating Tables in Mysql:

To delete all data in an existing table in the Mysql db, use the TRUNCATE TABLE Statement.

Syntax: TRUNCATE TABLE table_name;

Ex: truncate table Veena;

Handling larger Dataset:


1. Select the table and click on the 'Column Options.'
2. Click the arrow mark, which is available at the top right side. After clicking, it should display a downward arrow.
3. If necessary, modify the table name here.
4. Find the column name option and select column names one by one to remove any spaces, modify the column names,
or eliminate any extra spaces before or after column names under the column name option.
5. Once you have made the necessary changes, click on 'Apply,' which is available at the bottom right side. Then, click on
'Apply' again (this option will appear only if you have made any changes in the table; otherwise, it will not be visible).
Finally, click on 'Finish' and close the opened tab.

1. Register on the 'Kaggle' website.


2. Search for the required dataset.
3. Download the file and save it to your Desktop.
4. Log into 'SQL Workbench,' "create a database", and "use it". Click on 'Schemas' and 'Refresh' on the left side at the top
of the data.
5. Go to the created database, select the table under the created database, and right-click on the table. You will find
options; select the fourth option, "Table Data Import Wizard," and browse for the data.
6. Click on 'Next' > Click on 'Next' > Click on 'Next' > Click on 'Next' > Click on 'Next' > Click on 'Finish' > Click on 'Schemas'
and 'Refresh' on the left side at the top of the data. Go to 'Table' under the created Database. Click on the 'Arrow Mark'
under the table. Now we can be able to find the uploaded data with table name.

 Altering Tables in Mysql :

The Alter Statement is used to modify in an existing table.


Following are Some of the operations that the ALTER statement can perform :

 Adding columns to a table.


 Modifying columns in a table.
 Renaming column in a table
 Dropping a column from a table
 Renaming a table.

a) Adding Columns to a Table :

To add columns to an existing table in mysql you use the ALTER TABLE.
Two ways to add columns to a table:
1. Adding a single column to a table
2. Adding multiple columns to a table.

 Syntax for adding a single column to a table

Syntax : ALTER TABLE table_name


ADD
new-column-name, column- definition;

Example : alter table emp_data add total_salary int;

 Syntax for adding multiple columns to a table;

Syntax: ALTER TABLE table_name


ADD
new-column-name column_definition;

Example : alter table emp_data add Total_salary1 int after salary;

b) Dropping a Column in a Table :

To dropping a column an existing table in MySQL, we can use the ALTER TABLE.

Syntax for dropping a column in a table :

Syntax : ALTER TABLE table_name


DROP COLUMN column_name;

Example : alter table emp_data drop column total_salary2;

c) Renaming a Column in a Table :

To renaming a columns in an existing table in MySQL, we can use the ALTER TABLE.

Syntax for renaming a column in a table :


Syntax : ALTER TABLE table_name
CHANGE COLUMN
Original_name new_name
column-definition;

Example : alter table emp_data change column Role Designation Varchar(50);

d) Renaming a table :

To renaming a table in an existing database in MySQL we can use the ALTER TABLE.

Syntax for renaming a table:

Syntax : ALTER TABLE table name;


RENAME TO new_table_name;

Example : alter table emp_data rename to emp_details;

e) Modifying columns in a table :

To modify columns to an existing table in MySQL, we can use the ALTER TABLE, Here are some common modifications
you might want to perform.

 modify data types


 Add constraints

Syntax for modifying a single column in a Table

Syntax : ALTER TABLE table_name


MODIFY
column_name column_definition;

Example : alter table emp_data modify first_name varchar(5o) not null;

Comments In Mysql

Comments are used to explain sections of SQL Statements or to prevent execution of SQL statements.

Types of comments in MySQL:

 '- -' , '#' → Single_line comments


 /* , */ → Multi-Line comments

 Single line comments :

Single Line comments start with 2 hyphens(- -) or If the above option is not working, we can use (#).

Example : _ _ Select all ;


Select* from emp_details;

 Multi Line comments :

Multi line comments start with /* and end with */ Any text between /* and */ will be ignored.

Example : /* select all the columns of all the records in the emp data table: */ select * from emp_data;

Alias In SQL
Introduction to Alias :

 An alias is a temporary name assigned to a table, column for the purpose of an SQL query, and it exists only
for the duration of the query.
 The As keyword is used to make an alias.
 If an alias contains space, if must be enclosed in quotation marks.
 Aliases are typically used to make column names easier to understand.

Types of alias :

 column alias
 Table alias

 Column alias :

A column alias is a temporary name assigned to a column having a long technical name in order to simplify the
query output.

Syntax : SELECT

(Column_1) As

Column_alias_name

Example : Select first_name as empname from details;

 Table Alias :

Syntax : Select

[column_1] [column_2] FROM

Table_name As table_alias_name;

Example : select * from emp_data as emptable;

Order of Executions in SQL


 FROM → Select's the table from which the data is obtained
 JOIN→ Returns common data from a different Table
 WHERE → Applies a condition to filter data
 GROUP BY → Aggregates the data
 HAVING → Filters the aggregated data
 SELECT → Returns the required data

Constraints
Constraint is a condition that specifies the types of data that can be entered into a table.

Types of constraints in MySQL;

 NOT NULL
 Unique
 Default
 Primary key
 foreign key
 check
 NOT NULL Constraint :

NOT NULL constraint, which is used to prevents the NULL empty values in the column. It means there are no empty
values in the column.

Example : create table table_name (ID int, first_name Varchar (35) NOT NULL, city varchar (30));

Insert into notnull (ID, first_name, city) values (1, 'Promad, 'Bangalore’), (2, 'Ramu', ‘Tamil Nadu’);

 Unique constraints:

Unique constraint ensure that all values in a column are different, it should not be the duplicate values.

Syntax : Create table table_name (ID int NOT NULL, Name varchar(30), Age int, UNIQUE(ID));

 Default constraints :

Default Constraint which is used to fill the value automatically in a column.

Syntax : create table table_name (ID is NOT NULL, Name varchar(30), Age int, UNIQUE(ID), citizen varchar(10) default ‘India’);

 Check constraints :

Check constraint, which is used to verify the value being entered into a record.

Syntax : create table table_name (ID int NOT NULL, Name varchar(30), Age int, check (Age >=18);

 Primary key constraints :

Primary key is a constraint which is used to identify unique's each record in a table is called primary key. A table can
only have one primary key.

Rules for primary key :


 It should be an 'INTEGER’
 It should not be ‘NULL’
 It never be a 'DUPLICATE'
 It must be declaring as ‘PRIMARY KEY'
 It can be a ‘AUTO INCREMENT’ based on user requirement.

Syntax : create table Table-Name (ID int Primary key, Name varchar(30) NOT NULL, Age int);

 Foreign Key Constraints :


Foreign key constraint which is used to Connect two tables, It corresponds to the Primary key of a different table. Foreign Key act
as a primary key in parent table and foreign key as a child table.
Customer Orders Seller
Cust_ID Pk Order_ID Pk Seller_ID
Cust_Name Order_name Seller_Name
Cust_Gender Price Order_ID fk
Cust_ID fk Cust_ID fk

Example : Create table customer (cust-ID int primary Key , cust_name varchar(50), Cust-gender varchar(10);

Create table orders (order_ID int primary key, order_name varchar (50), price int, cust_ID int, foreign key
(cust_id) references customer Cust-ids);

Create table seller (Seller_ID int NOT NULL, Seller_Name varchar(50), Order_ID int, cust_ID int, Foreign_key
(order_ID) references orders (order_ID), Foreign_key(cust_ID) references customer (Cust_ID));
SQL FUNCTIONS
 SQL functions are basic subprograms used extensively to handle or manipulate data.
 SQL functions enhance database speed and performance.
 SQL functions are short programs with one or more input parameters but just one output value.

Types of functions :

 Aggregate functions
 Date and time functions
 Scalar functions
 String functions
 Miscellaneous

 Aggregate functions :

The aggregate functions allow performing the calculation on a set of values.

 COUNT()
 Sum()
 AVG()
 FIRST()
 LAST()
 MIN()
 MAX()

a) COUNT() :

Count function returns the total number of rows in a specified column or a table.

Syntax : SELECT COUNT (column_name) FROM table_name;

Example : select count(emp_ID) from Emp_details;

b) Sum() :

Sum function returns the sum of values from a particular column.

Syntax : SELECT SUM(column_name) From table_name;

Example : Select Sum (Salary) from emp_details;

c) AVG() :

Average function returns the average value of a particular column.

Syntax : Select AVG (column_name) From table_name;

Example : Select AVG (Salary) from emp_details;

d) Min() :

Min the functions returns the minimum value of the given column.

Syntax : SELECT MIN (column_name) FROM table name;

Example : Select min(salary) from emp_details;

e) Max() :

Max functions returns the Maximum value of the given column

Syntax : SELECT MAX(column_name) FROM table_name;


Example : Select max (Salary) from emp_details;

 Scalar functions :

The scalar functions returns a single value from an input value. It works on each record independently.
 ROUND()
 LEN()
 FORMAT()
 MID()
 NOW()
 UCASE()
 LCASE()

 ROUND() :

Round functions helps to round a value to a Specified number of places

Syntax : ROUND (Column_Name);

Example : Select round (121.23);

 LENGTH() :

Length function returns the total length of the given column.

Syntax : SELECT LENGTH (column_name) FROM table-name

Example : SELECT LENGTH (emp_id) from emp_details;

 FORMAT() :

Format function is used to format field value in the specified format.

Syntax : Select FORMAT (column_name, format) FROM table_name

Example : Select format (1201.302023, 2);

 MID() :

Mid function is used to retrieve the specified characters from the text field.

Syntax : Select MID (column_name, Start, length) FROM table-name

Example : Select Mid (first_name, 2,5) from emp_details;

 NOW() :

Now function is used to retrieve the system's current date and time.

Syntax : SELECT (Now)

Example : select now();

 UCASE() :

Ucase function converts the given column to uppercase.

Syntax : Select UCASE(column_name) FROM table_name;

Example : Select UCASE (First_name) from emp_details;


 LCASE() :

Lcase function converts the given column to lowercase.

Syntax : SELECT LCASE (Column_name) FROM table-name;

Example : Select Lcase(first_name) from emp_details;

 String functions :

The string functions are used for string manipulation.

 CONCAT()
 TRIM()

 Concat() :

concat function is used to combine one or more characters into a single string.

Syntax : SELECT CONCAT (String 1, string 2, _ _ _ _ _ N) FROM table_name;

Example : Select concat (first_name, " ", last_name) from emp_details;

 TRIM() :

Trim functions is used to remove the spaces from both sides of the given string.

Syntax : Select TRIM (string 1) FROM table name;

Example : select trim (‘veena’);

 Numeric Functions :

The numeric functions used to perform numeric manipulation of mathematical operations.

 ABS()
 MOD()
 CEIL()
 FLOOR()

You might also like