SQL interview questions and answers on creating and editing database and tables


Below are the interview questions for SQL basic and intermediate level SQL Developers, Administrators, and Testers. These questions are about Data Definition Language Commands, Operations, creating and editing database and tables. 

1) What are the Data Definition Language Commands and Operations?
Important Data Definition Language Commands
1) Create
2) Alter
3) Drop
4) Truncate
5) Rename

Important Data Definition Language Operations
1) Create a Database
2) Use Database
3) Rename a Database
4) Drop Database
5) Create a Table
6) Rename Table
7) Add a Column to exiting Table
8) Add multiple columns to existing Table
9) Modify an existing column
10) Rename a Column
11) Drop a Column
12) Truncate a Table
13) Drop a Table
Note: The above example is for MS SQL Server. Download and Install MS SQL Server Express Edition (It is Free Edition) and practice SQL Commands and Operations.

2) How to Create a Database?
Syntax:
Create Database databaseName;
Example:
Create Database proddb;

3) How to Select a Database?
Syntax
Use databaseName;
Example:
Use proddb;

4) How to Rename a Database?
Syntax
Alter Database databaseName Modify Name = newdatabseName;
Example:
Alter Database proddb Modify Name = production

5) How to Drop a Database?
Syntax:
Drop Database databaseName;
Example:
Drop Database proddb;

6) How to Create a Table?
Syntax:
Create Table tableName
(
column1_name dataType(size),
column2_name dataType(size),
.
.
.
);
Example:
Create Table Students
(
STID int,
STName char(50),
);

7) How to View Table info
Select * from Students

8) How to View Table Schema
Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'Students';

9) How to Rename a Table?
Syntax:
EXEC sp_rename 'old_tablename', 'new_tablename';
Example:
EXEC sp_rename 'Students', 'newStudents';

10) How to Add a Column to an existing Table?
Syntax:
Alter Table table_name add column_name dataType(size);
Example:
Alter Table newStudents add City char(50);

11) How to Add multiple columns to an existing Table?
Syntax:
Alter Table table_name add column1_name dataType(size), column2_name dataType(size);
Or
Alter Table table_name add
column1_name dataType(size),
column2_name dataType(size),
.
.;
Example:
Alter Table newStudents add add1 char(100), add2 char(70);
Or
Alter Table newStudents add
add3 char(100),
add4 char(70),
add5 char (100),
phone int;

12) How to Modify an existing column?
Syntax:
Alter Table table_name Alter Column column_name dataType(size);
Example:
Alter Table newStudents Alter Column add1 varchar(150);

13) How to Rename a Column?
Syntax:
EXEC sp_rename 'table_name.old_column_name', 'new_colum_name';
Example:
ExEC sp_rename 'newStudents.phone', 'mobile'

14) How to Drop a Column?
Syntax:
Alter Table table_name Drop Column column_name;
Example:
Alter Table newStudents Drop Column City;

15) How to Truncate a Table?
Truncate Table command is used to delete complete data from an existing table
Syntax:
Truncate Table table_name;
Example:
Truncate Table newStudents;

16) How to Drop a Table?
Drop Table command is used to delete complete Table (Data and Table Structure) from the Database.
Syntax:
Drop Table table_name;
Example:
Drop Table newStudents;

17) How to add new record into a Table?
Using INSERT INTO statement, we can insert new rows
Syntax:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN)




What Others are Reading Now...

0 Comment to "SQL interview questions and answers on creating and editing database and tables"

Post a Comment