• Prathamesh Sable

SQL Theory Questionnaire for Freshers

The below questions are at a very basic level but those are the base concepts that everyone should know. I have tried to list down some as per the ease.


Q #1) What is SQL?

Structured Query Language SQL is a database tool that is used to create and access the database to support software applications.


Q #2) What are tables in SQL?

The table is a collection of record and its information at a single view.


Q #3) What are the different types of statements supported by SQL?

There are 3 types of SQL statements:

a) DDL (Data Definition Language): It is used to define the database structure such as tables. It includes three statements such as CREATE, ALTER, and DROP.

Some of the DDL Commands are listed below:

CREATE: It is used for creating the table.

CREATE TABLE table_name
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),

ALTER: The ALTER table is used for modifying the existing table object in the database.

ALTER TABLE table_name
 ADD column_name datatype

OR

ALTER TABLE table_name
DROP COLUMN column_name

b) DML (Data Manipulation Language): These statements are used to manipulate the data in records. Commonly used DML statements are INSERT, UPDATE, and DELETE.

The SELECT statement is used as a partial DML statement, used to select all or relevant records in the table.


c) DCL (Data Control Language): These statements are used to set privileges such as GRANT and REVOKE database access permission to the specific user.


Q #4) How do we use the DISTINCT statement? What is its use?

The DISTINCT statement is used with the SELECT statement. If the record contains duplicate values then the DISTINCT statement is used to select different values among duplicate records.

Syntax:

SELECT DISTINCT column_name(s)
 FROM table_name;

Q #5) What are different Clauses used in SQL?

WHERE Clause: This clause is used to define the condition, extract and display only those records which fulfill the given condition.

Syntax:

SELECT column_name(s) 
 FROM table_name 
 WHERE condition;

GROUP BY Clause: It is used with SELECT statement to group the result of the executed query using the value specified in it. It matches the value with the column name in tables and groups the end result accordingly.

Syntax:

SELECT column_name(s)
 FROM table_name
 GROUP BY column_name;

HAVING clause: This clause is used in association with the GROUP BY clause. It is applied to each group of results or the entire result as a single group. It is much similar as WHERE clause but the only difference is you cannot use it without GROUP BY clause

Syntax:

SELECT column_name(s) 
 FROM table_name 
 GROUP BY column_name 
 HAVING condition;

ORDER BY clause: This clause is used to define the order of the query output either in ascending (ASC) or in descending (DESC). Ascending (ASC) is set as the default one but descending (DESC) is set explicitly.

Syntax:

SELECT column_name(s) 
 FROM table_name 
 WHERE condition 
 ORDER BY column_name ASC|DESC;

USING clause: USING clause comes in use while working with SQL JOIN. It is used to check equality based on columns when tables are joined. It can be used instead of the ON clause in JOIN.

Syntax:

SELECT column_name(s) 
 FROM table_name 
 JOIN table_name 
 USING (column_name);

Q #6) Why do we use SQL constraints? Which constraints we can use while creating a database in SQL?

Constraints are used to set the rules for all records in the table. If any constraints get violated then it can abort the action that caused it.

Constraints are defined while creating the database itself with the CREATE TABLE statement or even after the table is created once with the ALTER TABLE statement.

There are 5 major constraints are used in SQL, such as

  • NOT NULL: That indicates that the column must have some value and cannot be left NULL.

  • UNIQUE: This constraint is used to ensure that each row and column has a unique value and no value is being repeated in any other row or column.

  • PRIMARY KEY: This constraint is used in association with NOT NULL and UNIQUE constraints such as on one or the combination of more than one column to identify the particular record with a unique identity.

  • FOREIGN KEY: It is used to ensure the referential integrity of data in the table. It matches the value in one table with another using the PRIMARY KEY.

  • CHECK: It ensures whether the value in columns fulfills the specified condition.

Q #7) What are different JOINS used in SQL?

INNER JOIN: It is also known as SIMPLE JOIN which returns all rows from BOTH tables when it has at least one matching column.

Syntax:

SELECT column_name(s) 
 FROM table_name1 
 INNER JOIN table_name2 
 ON column_name1=column_name2;

LEFT JOIN (LEFT OUTER JOIN): This join returns all rows from the LEFT table and its matched rows from a RIGHT table.

Syntax:

SELECT column_name(s)
 FROM table_name1
 LEFT JOIN table_name2
 ON column_name1=column_name2;

RIGHT JOIN (RIGHT OUTER JOIN): This joins returns all rows from the RIGHT table and its matched rows from the LEFT table.

Syntax:

SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON column_name1=column_name2;

FULL JOIN (FULL OUTER JOIN): This joins returns all results when there is a match either in the RIGHT table or in the LEFT table.

Syntax:

SELECT column_name(s)
 FROM table_name1
 FULL OUTER JOIN table_name2
 ON column_name1=column_name2;

Q #8) What are transactions and their controls?

A transaction can be defined as the sequence task that is performed on databases in a logical manner to gain certain results. Operations like Creating, updating, deleting records performed in the database come from transactions. In simple words, we can say that a transaction means a group of SQL queries executed on database records.

There are 4 transaction controls such as:

  • COMMIT: It is used to save all changes made through the transaction.

  • ROLLBACK: It is used to roll back the transaction. All changes made by the transaction are reverted back and the database remains as before.

  • SET TRANSACTION: Set the name of the transaction.

  • SAVEPOINT: It is used to set the point where the transaction is to be rolled back.

Q #9) What are the properties of the transaction?

In the context of transaction processing, the acronym ACID refers to the four key properties of a transaction

  • Atomicity: Ensures the completeness of all transactions performed. Checks whether every transaction is completed successfully or not. If not, then the transaction is aborted at the failure point and the previous transaction is rolled back to its initial state as changes are undone.

  • Consistency: Ensures that all changes made through successful transactions are reflected properly on the database.

  • Isolation: Ensures that all transactions are performed independently and changes made by one transaction are not reflected on others.

  • Durability: Ensures that the changes made in the database with committed transactions persist as it is even after a system failure.

Q #10) How many Aggregate functions are available in SQL?

SQL Aggregate functions determine and calculate values from multiple columns in a table and return a single value. There are 7 aggregate functions in SQL:

  • AVG(): Returns the average value from specified columns.

  • COUNT(): Returns number of table rows.

  • MAX(): Returns the largest value among the records.

  • MIN(): Returns smallest value among the records.

  • SUM(): Returns the sum of specified column values.

  • FIRST(): Returns the first value.

  • LAST(): Returns last value.

Q #11) What are Scalar functions in SQL?

Scalar functions are used to return a single value based on the input values. Scalar Functions are as follows:

  • UCASE(): Converts the specified field in the upper case.

  • LCASE(): Converts the specified field in lower case.

  • MID(): Extracts and returns character from the text field.

  • FORMAT(): Specifies the display format.

  • LEN(): Specifies the length of the text field.

  • ROUND(): Rounds up the decimal field value to a number.

Q #12) What are triggers?

Triggers in SQL is kind of stored procedures used to create a response to a specific action performed on the table such as INSERT, UPDATE or DELETE. You can invoke triggers explicitly on the table in the database.

Action and Event are two main components of SQL triggers. When certain actions are performed, the event occurs in response to that action.

CREATE TRIGGER name {BEFORE|AFTER} (event [OR..]}
ON table_name [FOR [EACH] {ROW|STATEMENT}]
EXECUTEPROCEDUREfunctionname {arguments}

Q #13) What is View in SQL?

A View can be defined as a virtual table that contains rows and columns with fields from one or more tables.

CREATE VIEW view_name AS
SELECT column_name(s) 
FROM table_name 
WHERE condition

Q #14) How we can update the view?

SQL CREATE and REPLACE can be used for updating the view.

Execute the below query to update the created view.

CREATE OR REPLACE VIEW view_name AS
 SELECT column_name(s)
 FROM table_name
 WHERE condition

Q #15) Explain the working of SQL Privileges?

Answer: SQL GRANT and REVOKE commands are used to implement privileges in SQL multiple user environments. The administrator of the database can grant or revoke privileges to or from users of database objects by using commands like SELECT, INSERT, UPDATE, DELETE, ALL, etc.

GRANT Command: This command is used to provide database access to users other than the administrator.

GRANT privilege_name
 ON object_name
 TO {user_name|PUBLIC|role_name}
 [WITH GRANT OPTION];

In the above syntax, the GRANT option indicates that the user can grant access to another user too.

REVOKE command: This command is used to provide database deny or remove access to database objects.

REVOKE privilege_name
 ON object_name
 FROM {user_name|PUBLIC|role_name};

Q #16) How many types of Privileges are available in SQL?

There are two types of privileges used in SQL, such as

  • System privilege: System privilege deals with the object of a particular type and provides users the right to perform one or more actions on it. These actions include performing administrative tasks, ALTER ANY INDEX, ALTER ANY CACHE GROUP CREATE/ALTER/DELETE TABLE, CREATE/ALTER/DELETE VIEW etc.

  • Object privilege: This allows to perform actions on an object or object of another user(s) viz. table, view, indexes etc. Some of the object privileges are EXECUTE, INSERT, UPDATE, DELETE, SELECT, FLUSH, LOAD, INDEX, REFERENCES etc.

Q #17) What is SQL Injection?

SQL Injection is a type of database attack technique where malicious SQL statements are inserted into an entry field of database in a way that once it is executed, the database is exposed to an attacker for the attack. This technique is usually used for attacking data-driven applications to have access to sensitive data and perform administrative tasks on databases. Check https://www.w3schools.com/sql/sql_injection.asp for more details

Syntax: SELECT column_name(s) FROM table_name WHERE condition;
E.g. which creates a SELECT statement by adding a variable (txtUserId) to a select string. The variable is fetched from user input (getRequestString):
txtUserId = getRequestString("UserId");
txtSQL = "SELECT * FROM Users WHERE UserId = " + txtUserId;

Q #18) What is SQL Sandbox in SQL Server?

SQL Sandbox is a safe place in the SQL server environment where untrusted scripts are executed. There are 3 types of SQL sandbox:

  • Safe Access Sandbox: Here a user can perform SQL operations such as creating stored procedures, triggers etc. but cannot have access to the memory as well as cannot create files.

  • External Access Sandbox: Users can access files without having the right to manipulate the memory allocation.

  • Unsafe Access Sandbox: This contains untrusted codes where a user can have access to memory.

Q #19) What is the difference between SQL and PL/SQL?

SQL is a Structured Query Language to create and access databases whereas PL/SQL comes with procedural concepts of programming languages.


Q #20) What is the difference between SQL and MySQL?

SQL is a Structured Query Language that is used for manipulating and accessing the relational database. On the other hand, MySQL itself is a relational database that uses SQL as the standard database language.


Q #21) What is the use of the NVL function?

NVL function is used to convert the null value to its actual value.


Q #22) What is the Cartesian product of the table?

The output of Cross Join is called a Cartesian product. It returns rows combining each row from the first table with each row of the second table. For Example, if we join two tables having 15 and 20 columns the Cartesian product of two tables will be 15×20=300 rows.


Q #23) What do you mean by Subquery?

Query within another query is called as Subquery. A subquery is called inner query which returns output that is to be used by another query.


Q #24) How many row comparison operators are used while working with a subquery?

There are 3-row comparison operators that are used in subqueries such as IN, ANY and ALL.


Q #25) What is the difference between clustered and non-clustered indexes?

The differences between the two are as follows:

  • One table can have only one clustered index but multiple non-clustered indexes.

  • Clustered indexes can be read rapidly rather than non-clustered indexes.

  • Clustered indexes store data physically in the table or view whereas, non-clustered indexes do not store data in the table as it has separate structure from the data row.

Q #26) What is the difference between DELETE and TRUNCATE?

  • The basic difference in both is DELETE command is DML command and the TRUNCATE command is DDL.

  • 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 WHERE clause but cannot use the TRUNCATE command with it.

Q #27) What is the difference between DROP and TRUNCATE?

Answer: 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.


Q #28) How to write a query to show the details of a student from Students table whose name start with K?

SELECT * FROM Student WHERE Student_Name like K%;

Here ‘like’ operator is used to perform pattern matching.


Q #29) What is the difference between Nested Subquery and Correlated Subquery?

Subquery within another subquery is called Nested Subquery. If the output of a subquery depends on column values of the parent query table then the query is called Correlated Subquery.

SELECT adminid(SELEC   Firstname+' '+Lastname  FROM Employee WHERE
 empid=emp. adminid)AS EmpAdminId FROM Employee;

The result of the query is the details of an employee from the Employee table.


Q #30) What is Normalization? How many Normalization forms are there?

Normalization is used to organize the data in such a manner that data redundancy will never occur in the database and avoid insert, update and delete anomalies.

There are 5 forms of Normalization:

  • First Normal Form (1NF): It removes all duplicate columns from the table. It creates a table for related data and identifies unique column values.

  • First Normal Form (2NF): Follows 1NF and creates and places data subsets in an individual table and defines the relationship between tables using the primary key.

  • Third Normal Form (3NF): Follows 2NF and removes those columns which are not related through the primary key.

  • Fourth Normal Form (4NF): Follows 3NF and does not define multi-valued dependencies. 4NF is also known as BCNF.

Q #31) What is a Relationship? How many types of Relationships are there?

The relationship can be defined as the connection between more than one table in the database.

There are 4 types of relationships:

  • One to One Relationship

  • Many to One Relationship

  • Many to Many Relationship

  • One to Many Relationship

Q #32) What do you mean by Stored Procedures? How do we use it?

A stored procedure is a collection of SQL statements that can be used as a function to access the database. We can create these stored procedures earlier before using it and can execute them wherever required by applying some conditional logic to it. Stored procedures are also used to reduce network traffic and improve performance.

CREATE Procedure Procedure_Name
 (
 //Parameters
 )
 AS
 BEGIN
 SQL statements in stored procedures to update/retrieve records
 END

Q #33) State some properties of Relational databases?

Properties are as follows:

  • In relational databases, each column should have a unique name.

  • The sequence of rows and columns in relational databases is insignificant.

  • All values are atomic and each row is unique.

Q #34) What are Nested Triggers?

Triggers may implement data modification logic by using INSERT, UPDATE, and DELETE statements. These triggers that contain data modification logic and find other triggers for data modification are called Nested Triggers.


Q #35) What is a Cursor?

A cursor is a database object which is used to manipulate data in a row-to-row manner.

Cursor follows steps as given below:

  • Declare Cursor

  • Open Cursor

  • Retrieve row from the Cursor

  • Process the row

  • Close Cursor

  • Deallocate Cursor

Q #36) What is Collation?

Collation is a set of rules that check how the data is sorted by comparing it. Such as character data is stored using correct character sequence along with case sensitivity, type, and accent.


Q #37) What do we need to check in Database Testing?

In Database testing, the following thing is required to be tested:

  • Database connectivity

  • Constraint check

  • Required application field and its size

  • Data Retrieval and processing with DML operations

  • Stored Procedures

  • Functional flow

Q #38) What is Database White Box Testing?

Database White Box testing involves:

  • Database Consistency and ACID properties

  • Database triggers and logical views

  • Decision Coverage, Condition Coverage, and Statement Coverage

  • Database Tables, Data Model, and Database Schema

  • Referential integrity rules

Q #39) What is Database Black Box Testing?

Database Black Box testing involves:

  • Data Mapping

  • Data stored and retrieved

  • Use of Black Box testing techniques such as Equivalence Partitioning and Boundary Value Analysis (BVA)

Q #40) What are Indexes in SQL?

The index can be defined as the way to retrieve the data more quickly. We can define indexes using CREATE statements.

CREATE INDEX index_name
 ON table_name (column_name)
Further, we can also create a Unique Index using the following syntax:
CREATE UNIQUE INDEX index_name
 ON table_name (column_name)

Q #41) Query to select random rows from a table?

Using a SAMPLE clause we can select random rows.

SELECT * FROM table_name SAMPLE(10);

Q #42) Can we rename a column in the output of the SQL query?

Yes, using the following syntax we can do this.

SELECT column_name AS new_name FROM table_name;

Q #43) Suppose a Student column has two columns, Name and Marks. How to get names and marks of the top three students.

SELECT Name, Marks FROM Student s1 where 3 <= (SELECT COUNT(*) FROM Students s2 WHERE s1.marks = s2.marks)

Q #44) What is the difference between Local and Global temporary tables?

Answer: If defined inside a compound statement a local temporary table exists only for the duration of that statement but a global temporary table exists permanently in the DB but its rows disappear when the connection is closed.


Q #45) What is CTE?

Answer: A CTE or common table expression is an expression that contains temporary result set which is defined in a SQL statement.


CONCLUSION: I guess this will help you understand the concepts of SQL. Rest is all practice.

Feel free to add comments and suggestions. Do like the post if you agree with the questions

Also refer to https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all as its one of the best sites for practice and tutorials

40 views0 comments

Recent Posts

See All