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

DatabaseInterview

DatabaseInterview good resource

Uploaded by

Ruchi Binayake
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DatabaseInterview

DatabaseInterview good resource

Uploaded by

Ruchi Binayake
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

What is SQL?

SQL stands for Structured Query Language. It is a language used to interact with the database, i.e to
create a database, to create a table in the database, to retrieve data or update a table in the
database

What is a database?

A database is an organized collection of data, so that it can be easily accessed and managed.

Does SQL support programming language features?

yes It is true that SQL is a language, but it is not support the programming features like conditional
statements, for loops or if..else, it is command based language nd used to create delete the
database.

What is the difference between CHAR and VARCHAR2 data types in SQL?

Char has a fixed size, but varchar has a variable size.

Database:
A database is a collection of data that is organized, which is also called structured data.
Databases can contain any type of data, such as numbers, words, images, videos, and files.

A Database Management System (DBMS) :


is a software system that is designed to manage and organize data in a structured manner. It
allows users to create, modify, and query a database
Database Languages
 Data Definition Language: used to define and manage the structure of a database
 Data Manipulation Language: used to manipulate the data
 Data Control Language:
 Transactional Control Language: used to manage transactions in a database

Main DDL Commands:


1. CREATE
o Used to create new database objects like tables, views, or schemas.
Syntax:
sql
Copy code
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
...
);
Example:
sql
Copy code
CREATE TABLE Employees (
id INT PRIMARY KEY,
name VARCHAR(50),
salary DECIMAL(10, 2)
);

2. ALTER
o Used to modify the structure of an existing object, such as adding or dropping
columns, renaming columns, or altering constraints.
Syntax:
sql
Copy code
ALTER TABLE table_name
ADD column_name datatype;
Example:
sql
Copy code
ALTER TABLE Employees
ADD age INT;

3. DROP
o Used to delete database objects, such as tables, views, or schemas. Once
dropped, the object and its data are permanently removed.
Syntax:
sql
Copy code
DROP TABLE table_name;
Example:
sql
Copy code
DROP TABLE Employees;

4. TRUNCATE
o Removes all rows from a table without logging individual row deletions. The
table structure remains intact.
Syntax:
sql
Copy code
TRUNCATE TABLE table_name;
Example:
sql
Copy code
TRUNCATE TABLE Employees;

5. RENAME
o Changes the name of an existing database object.
Syntax:
sql
Copy code
ALTER TABLE old_table_name
RENAME TO new_table_name;
Example:
sql
Copy code
ALTER TABLE Employees
RENAME TO Staff;
DML Commands in SQL
DML (Data Manipulation Language) is a subset of SQL commands used to manipulate the
data stored in database tables. These commands are focused on retrieving, inserting,
updating, and deleting data from tables.

Main DML Commands


1. SELECT
o Used to retrieve data from one or more tables in a database.
Syntax:
sql
Copy code
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
sql
Copy code
SELECT name, salary
FROM Employees
WHERE salary > 50000;
Output: A result set showing the name and salary of employees with salaries greater than
50,000.

2. INSERT
o Adds new rows (records) to a table.
Syntax:
sql
Copy code
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example:
sql
Copy code
INSERT INTO Employees (id, name, salary)
VALUES (1, 'John Doe', 50000);
Effect: A new row is added to the Employees table.

3. UPDATE
o Modifies existing records in a table.
Syntax:
sql
Copy code
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
sql
Copy code
UPDATE Employees
SET salary = 55000
WHERE id = 1;
Effect: The salary of the employee with id = 1 is updated to 55,000.

4. DELETE
o Removes rows from a table based on a condition.
Syntax:
sql
Copy code
DELETE FROM table_name
WHERE condition;
Example:
sql
Copy code
DELETE FROM Employees
WHERE id = 1;
Effect: The row with id = 1 is removed from the Employees table.

TCL Commands in SQL


TCL (Transaction Control Language) is a subset of SQL commands used to manage
transactions in a database. Transactions are logical units of work that can consist of one or
more SQL DML commands (like INSERT, UPDATE, DELETE) executed as a single operation. TCL
ensures data integrity and consistency in case of success or failure.

1. COMMIT
o Saves all the changes made in the current transaction to the database
permanently.
Syntax:
sql
Copy code
COMMIT;
Example:
sql
Copy code
BEGIN TRANSACTION;
INSERT INTO Employees (id, name, salary) VALUES (1, 'John Doe', 50000);
COMMIT;
Effect: The changes made by the INSERT statement are saved permanently.
2. ROLLBACK
o Undoes all the changes made in the current transaction. This is used to revert
changes in case of an error or when the operation needs to be canceled.
Syntax:
sql
Copy code
ROLLBACK;
Example:
sql
Copy code
BEGIN TRANSACTION;
UPDATE Employees SET salary = 60000 WHERE id = 1;
ROLLBACK;
Effect: The UPDATE operation is canceled, and the original salary remains unchanged.
1. Data Definition Language

DDL is the short name for Data Definition Language, which deals with database schemas and
descriptions, of how the data should reside in the database.

SQL statements:
 CREATE: to create a database and its objects like (table, index, views, store
procedure, function, and triggers)
 ALTER: alters the structure of the existing database
 DROP: delete objects from the database
 TRUNCATE: remove all records from a table, including all spaces allocated for the
records are removed
 COMMENT: add comments to the data dictionary
 RENAME: rename an object

2.Data Manipulation Language:


DML is the short name for Data Manipulation Language which deals with data manipulation
 SELECT: retrieve data from a database
 INSERT: insert data into a table
 UPDATE: updates existing data within a table
 DELETE: Delete all records from a database table

3.Data Control Language


DCL is short for Data Control Language which acts as an access specifier to the database.
 GRANT: grant permissions to the user for running DML(SELECT, INSERT, DELETE,…)
commands on the table
 REVOKE: revoke permissions to the user for running DML(SELECT, INSERT, DELETE,…)
command on the specified table

4.Transactional Control Language


TCL is short for Transactional Control Language which acts as an manager for all types of
transactional data and all transactions.
 Roll Back: Used to cancel or Undo changes made in the database
 Commit: It is used to apply or save changes in the database
 Save Point: It is used to save the data on the temporary basis in the database

SQL is a programming language used to interact with relational database.


Perform CRUD:
Create
Read
Update
Delete
How can you create a backup of a table?
Answer:
Use the CREATE TABLE ... AS statement.
Example:
sql
Copy code
CREATE TABLE Employees_backup AS
SELECT * FROM Employees;

You might also like