Basic Level SQL Interview Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18
At a glance
Powered by AI
The key takeaways are that SQL is a standard language for accessing and managing data in a relational database, while MySQL is a specific database management system. SQL has different subsets like DDL, DML and DCL for defining, manipulating and controlling access to data respectively.

The different types of joins in SQL are inner join, left join, right join and full join. Inner join returns rows where the join condition is satisfied. Left join returns all rows from the left table and matching rows from the right table. Right join is opposite of left join. Full join returns all rows when there is a match in either table.

CHAR is used for fixed length character strings while VARCHAR2 is used for variable length character strings. CHAR will pad empty spaces for values shorter than the specified length whereas VARCHAR2 will only use the actual length of the stored value.

Q1. What is the difference between SQL and MySQL?

SQL MySQL

SQL is a standard language which stands for


Structured Query Language based on the MySQL is a database management system.
English language
MySQL is an RDMS (Relational Database
SQL is the core of the relational database which
Management System) such as SQL Server,
is used for accessing and managing database
Informix etc.

Q2. What are the different subsets of SQL?


 Data Definition Language (DDL) – It allows you to perform various operations on the
database such as CREATE, ALTER, and DELETE objects.
 Data Manipulation Language(DML) – It allows you to access and manipulate data. It
helps you to insert, update, delete and retrieve data from the database.
 Data Control Language(DCL) – It allows you to control access to the database. Example
– Grant, Revoke access permissions.

Q3. What do you mean by DBMS? What are its different types?
A Database Management System (DBMS) is a  software application that interacts with the
user, applications, and the database itself to capture and analyze data. A database is a structured
collection of data. 
A DBMS allows a user to interact with the database. The data stored in the database can be
modified, retrieved and deleted and can be of any type like strings, numbers, images, etc.
There are two types of DBMS:
 Relational Database Management System: The data is stored in relations (tables).
Example – MySQL.
 Non-Relational Database Management System: There is no concept of relations, tuples
and attributes.  Example – MongoDB

Q4. What do you mean by table and field in SQL?


A table refers to a collection of data in an organised manner in form of rows and columns. A
field refers to the number of columns in a table. For example:
Table:StudentInformation
Field: Stu Id, Stu Name, Stu Marks

Q5. What are joins in SQL?


A JOIN clause is used to combine rows from two or more tables, based on a related column
between them. It is used to merge two tables or retrieve data from there. There are 4 types of
joins, as you can refer to below:
 Inner join: Inner Join in SQL is the most common type of join. It is used to return all the
rows from multiple tables where the join condition is satisfied. 
 Left Join:  Left Join in SQL is used to return all the rows from the left table but only the
matching rows from the right table where the join condition is fulfilled.
 Right Join: Right Join in SQL is used to return all the rows from the right table but only
the matching rows from the left table where the join condition is fulfilled.
 Full Join: Full join returns all the records when there is a match in any of the tables.
Therefore, it returns all the rows from the left-hand side table and all the rows from the
right-hand side table.

Q6. What is the difference between CHAR and VARCHAR2 datatype in SQL?


Both Char and Varchar2 are used for characters datatype but varchar2 is used for character
strings of variable length whereas Char is used for strings of fixed length. For example, char(10)
can only store 10 characters and will not be able to store a string of any other length whereas
varchar2(10) can store any length i.e 6,8,2 in this variable.

Q7. What is a Primary key?

 A Primary key in SQL is a column (or collection of columns) or a set of columns that
uniquely identifies each row in the table.
 Uniquely identifies a single row in the table
 Null values not allowed
Example- In the Student table, Stu_ID is the primary key.

Q8. What are Constraints?


Constraints in SQL are used to specify the limit on the data type of the table. It can be specified
while creating or altering the table statement. The sample of constraints are:
 NOT NULL
 CHECK
 DEFAULT
 UNIQUE
 PRIMARY KEY
 FOREIGN KEY

Q9. What is the difference between DELETE and TRUNCATE statements?


DELETE TRUNCATE

Delete command is used to delete a row in a Truncate is used to delete all the rows from a
table. table.
You can rollback data after using delete
You cannot rollback data.
statement.
It is a DML command. It is a DDL command.
It is slower than truncate statement. It is faster.

Q10. What is a Unique key?


 Uniquely identifies a single row in the table.
 Multiple values allowed per table.
 Null values allowed.

Q11. What is a Foreign key in SQL?


 Foreign key maintains referential integrity by enforcing a link between the data in two
tables.
 The foreign key in the child table references the primary key in the parent table.
 The foreign key constraint prevents actions that would destroy links between the child
and parent tables.

Q12. What do you mean by data integrity? 


Data Integrity defines the accuracy as well as the consistency of the data stored in a database. It
also defines integrity constraints to enforce business rules on the data when it is entered into an
application or a database.

Q13. What is the difference between clustered and non-clustered index in SQL?
The differences between the clustered and non clustered index in SQL are :
1. Clustered index is used for easy retrieval of data from the database and its faster whereas
reading from non clustered index is relatively slower.
2. Clustered index alters the way records are stored in a database as it sorts out rows by the
column which is set to be clustered index whereas in a non clustered index, it does not
alter the way it was stored but it creates a separate object within a table which points back
to the original table rows after searching.
3. One table can only have one clustered index whereas it can have many non clustered
index.

Q14. Write a SQL query to display the current date?


In SQL, there is a built-in function called GetDate() which helps to return the current
timestamp/date.

Q15.What do you understand by query optimization?


The phase that identifies a plan for evaluation query which has the least estimated cost is known
as query optimization.
The advantages of query optimization are as follows:
 The output is provided faster
 A larger number of queries can be executed in less time
 Reduces time and space complexity

Q16. What do you mean by Denormalization?


Denormalization refers to a technique which is used to access data from higher to lower forms of
a database. It helps the database managers to increase the performance of the entire infrastructure
as it introduces redundancy into a table. It adds the redundant data into a table by incorporating
database queries that combine data from various tables into a single table.

Q17. What are Entities and Relationships?


Entities:  A person, place, or thing in the real world about which data can be stored in a
database. Tables store data that represents one type of entity. For example – A bank database has
a customer table to store customer information. The customer table stores this information as a
set of attributes (columns within the table) for each customer.
Relationships: Relation or links between entities that have something to do with each other. For
example – The customer name is related to the customer account number and contact
information, which might be in the same table. There can also be relationships between separate
tables (for example, customer to accounts).

Q18. What is an Index?
An index refers to a performance tuning method of allowing faster retrieval of records from the
table. An index creates an entry for each value and hence it will be faster to retrieve data.

Q19. Explain different types of index in SQL.


There are three types of index in SQL namely:
Unique Index:
This index does not allow the field to have duplicate values if the column is unique indexed. If a
primary key is defined, a unique index can be applied automatically.
Clustered Index:
This index reorders the physical order of the table and searches based on the basis of key values.
Each table can only have one clustered index.
Non-Clustered Index:
Non-Clustered Index does not alter the physical order of the table and maintains a logical order
of the data. Each table can have many nonclustered indexes.

Q20. What is Normalization and what are the advantages of it?


Normalization in SQL is the process of organizing data to avoid duplication and redundancy.
Some of the advantages are:
 Better Database organization
 More Tables with smaller rows
 Efficient data access
 Greater Flexibility for Queries
 Quickly find the information
 Easier to implement Security
 Allows easy modification
 Reduction of redundant and duplicate data
 More Compact Database
 Ensure Consistent data after modification
Apart from this SQL Interview Questions Blog, if you want to get trained from professionals on
this technology, you can opt for structured training from edureka! 

Q21. What is the difference between DROP and TRUNCATE commands?


DROP command removes a table and it cannot be rolled back from the database whereas
TRUNCATE command removes all the rows from the table.

Q22. Explain different types of Normalization.


There are many successive levels of normalization. These are called normal forms. Each
consecutive normal form depends on the previous one.The first three normal forms are usually
adequate.
 First Normal Form (1NF) – No repeating groups within rows
 Second Normal Form (2NF) – Every non-key (supporting) column value is dependent on
the whole primary key.
 Third Normal Form (3NF) – Dependent solely on the primary key and no other non-key
(supporting) column value.

Q23. What is the ACID property in a database?


ACID stands for Atomicity, Consistency, Isolation, Durability. It is used to ensure that the data
transactions are processed reliably in a database system. 
 Atomicity: Atomicity refers to the transactions that are completely done or failed where
transaction refers to a single logical operation of a data. It means if one part of any
transaction fails, the entire transaction fails and the database state is left unchanged.
 Consistency: Consistency ensures that the data must meet all the validation rules. In
simple words,  you can say that your transaction never leaves the database without
completing its state.
 Isolation: The main goal of isolation is concurrency control.
 Durability: Durability means that if a transaction has been committed, it will occur
whatever may come in between such as power loss, crash or any sort of error.

Q24. What do you mean by “Trigger” in SQL?


Trigger in SQL is are a special type of stored procedures that are defined to execute
automatically in place or after data modifications. It allows you to execute a batch of code when
an insert, update or any other query is executed against a specific table.

Q25. What are the different operators available in SQL?


There are three operators available in SQL, namely:
1. Arithmetic Operators
2. Logical Operators
3. Comparison Operators
Apart from this SQL Interview Questions blog, if you want to get trained from professionals on
this technology, you can opt for structured training from edureka! 

Q26.  Are NULL values same as that of zero or a blank space? 


A NULL value is not at all same as that of zero or a blank space. NULL value represents a value
which is unavailable, unknown, assigned or not applicable whereas a zero is a number and blank
space is a character.

Q27. What is the difference between cross join and natural join?
The cross join produces the cross product or Cartesian product of two tables whereas the natural
join is based on all the columns having the same name and data types in both the tables.

Q28. What is subquery in SQL?


A subquery is a query inside another query where a query is defined to retrieve data or
information back from the database. In a subquery, the outer query is called as the main query
whereas the inner query is called subquery. Subqueries are always executed first and the result of
the subquery is passed on to the main query. It can be nested inside a SELECT, UPDATE or any
other query. A subquery can also use any comparison operators such as >,< or =.

Q29. What are the different types of a subquery?


There are two types of subquery namely, Correlated and Non-Correlated.
Correlated subquery: These are queries which select the data from a table referenced in the
outer query. It is not considered as an independent query as it refers to another table and refers
the column in a table.
Non-Correlated subquery: This query is an independent query where the output of subquery is
substituted in the main query.

Q30. List the ways to get the count of records in a table?


To count the number of records in a table in SQL, you can use the below commands:
SELECT * FROM table1

SELECT COUNT(*) FROM table1

SELECT rows FROM sysindexes WHERE id = OBJECT_ID(table1) AND indid < 2

Apart from this SQL Interview Questions Blog, if you want to get trained from professionals on
this technology, you can opt for structured training from edureka! 

Q31. Write a SQL query to find the names of employees that begin with ‘A’?
To display name of the employees that begin with ‘A’, type in the below command:
1 SELECT * FROM Table_name WHERE EmpName like 'A%'

Q32. Write a SQL query to get the third-highest salary of an employee from
employee_table?

1 SELECT TOP 1 salary


2 FROM(
3 SELECT TOP 3 salary
4 FROM employee_table
5 ORDER BY salary DESC) AS emp
6 ORDER BY salary ASC;

Q33. What is the need for group functions in SQL? 


Group functions work on the set of rows and return one result per group. Some of the commonly
used group functions are: AVG, COUNT, MAX, MIN, SUM, VARIANCE.

Q34 . What is a Relationship and what are they?


Relation or links are between entities that have something to do with each other. Relationships
are defined as the connection between the tables in a database. There are various relationships,
namely:
 One to One Relationship.
 One to Many Relationship.
 Many to One Relationship.
 Self-Referencing Relationship.

Q35.  How can you insert NULL values in a column while inserting the data?
NULL values in SQL can be inserted in the following ways:
 Implicitly by omitting column from column list.
 Explicitly by specifying NULL keyword in the VALUES clause

Q36. What is the main difference between ‘BETWEEN’ and ‘IN’ condition operators?
BETWEEN operator is used to display rows based on a range of values in a row whereas the IN
condition operator is used to check for values contained in a specific set of values.
Example of BETWEEN:
SELECT * FROM Students where ROLL_NO BETWEEN 10 AND 50;
Example of IN:
SELECT * FROM students where ROLL_NO IN (8,15,25);

Q37. Why are SQL functions used?


SQL functions are used for the following purposes:
 To perform some calculations on the data
 To modify individual data items
 To manipulate the output
 To format dates and numbers
 To convert the data types

Q38. What is the need for MERGE statement?


This statement allows conditional update or insertion of data into a table. It performs an
UPDATE if a row exists, or an INSERT if the row does not exist.

Q39. What do you mean by recursive stored procedure?


Recursive stored procedure refers to a stored procedure which calls by itself until it reaches some
boundary condition. This recursive function or procedure helps the programmers to use the same
set of code n number of times.

Q40. What is CLAUSE in SQL?


SQL clause helps to limit the result set by providing a condition to the query. A clause helps to
filter the rows from the entire set of records.
For example – WHERE, HAVING clause.
Apart from this SQL Interview Questions Blog, if you want to get trained from professionals on
this technology, you can opt for a structured training from edureka! Click below to know more.

Q41. What is the difference between ‘HAVING’ CLAUSE and a ‘WHERE’ CLAUSE?
HAVING clause can be used only with SELECT statement. It is usually used in a GROUP BY
clause and whenever GROUP BY is not used, HAVING behaves like a WHERE clause.
Having Clause is only used with the GROUP BY function in a query whereas WHERE Clause is
applied to each row before they are a part of the GROUP BY function in a query.

Q42. List the ways in which  Dynamic SQL can be executed?


Following are the ways in which dynamic SQL can be executed:
 Write a query with parameters.
 Using EXEC.
 Using sp_executesql.

Q43. What are the various levels of constraints?


Constraints are the representation of a column to enforce data entity and consistency. There are
two levels  of a constraint, namely:
 column level constraint
 table level constraint
Q44. How can you fetch common records from two tables?
You can fetch common records from two tables using INTERSECT. For example:
1Select studentID from student. <strong>INTERSECT </strong> Select StudentID from Exam
Q45. List some case manipulation functions in SQL?
There are three case manipulation functions in SQL, namely:
 LOWER: This function returns the string in lowercase. It takes a string as an argument
and returns it by converting it into lower case. Syntax:
LOWER(‘string’)
 UPPER: This function returns the string in uppercase. It takes a string as an argument and
returns it by converting it into uppercase. Syntax:
UPPER(‘string’)
 INITCAP: This function returns the string with the first letter in uppercase and rest of the
letters in lowercase. Syntax:
INITCAP(‘string’)

Apart from this SQL Interview Questions blog, if you want to get trained from professionals on
this technology, you can opt for a structured training from edureka! Click below to know more.

 Q46. What are the different set operators available in SQL?


Some of the available set operators are – Union, Intersect or Minus operators.

Q47. What is an ALIAS command?


ALIAS command in SQL is the name that can be given to any table or a column. This alias name
can be referred in WHERE clause to identify a particular table or a column.
For example-
Select emp.empID, dept.Result from employee emp, department as dept where
emp.empID=dept.empID
In the above example, emp refers to alias name for employee table and dept refers to alias name
for department table.

Q48. What are aggregate and scalar functions?


Aggregate functions are used to evaluate mathematical calculation and returns a single value.
These calculations are done from the columns in a table. For example- max(),count() are
calculated with respect to numeric.
Scalar functions return a single value based on the input value. For example – UCASE(), NOW()
are calculated with respect to string.

Q49. How can you fetch alternate records from a table?


You can fetch alternate records i.e both odd and even row numbers. For example- To display
even numbers, use the following command:
Select studentId from (Select rowno, studentId from student) where mod(rowno,2)=0
Now, to display odd numbers:
Select studentId from (Select rowno, studentId from student) where mod(rowno,2)=1
Q50. Name the operator which is used in the query for pattern matching?
LIKE operator is used for pattern matching, and it can be used as -.
1. % – It matches zero or more characters.
For example- select * from students where studentname like ‘a%’
_ (Underscore) – it matches exactly one character.
For example- select * from student where studentname like ‘abc_’
Apart from this SQL Interview Questions Blog, if you want to get trained from professionals on
this technology, you can opt for structured training from edureka! 

Q51. How can you select unique records from a table?


You can select unique records from a table by using the DISTINCT keyword.
Select DISTINCT studentID from Student
Using this command, it will print unique student id from the table Student.

Q52. How can you fetch first 5 characters of the string?


There are a lot of ways to fetch characters from a string. For example:
Select SUBSTRING(StudentName,1,5) as studentname from student

Q53. What is the main difference between SQL and PL/SQL?


SQL is a query language that allows you to issue a single query or execute a single
insert/update/delete whereas PL/SQL is Oracle’s “Procedural Language” SQL, which allows you
to write a full program (loops, variables, etc.) to accomplish multiple operations such as
selects/inserts/updates/deletes. 

Q54. What is a View?


A view is a virtual table which consists of a subset of data contained in a table. Since views are
not present, it takes less space to store. View can have data of one or more tables combined and
it depends on the relationship.

Q55. What are Views used for?


A view refers to a logical snapshot based on a table or another view. It is used for the following
reasons:
 Restricting access to data.
 Making complex queries simple.
 Ensuring data independence.
 Providing different views of same data.

Q56. What is a Stored Procedure?


A Stored Procedure is a function which consists of many SQL statements to access the database
system. Several SQL statements are consolidated into a stored procedure and execute them
whenever and wherever required which saves time and avoid writing code again and again.
Q57. List some advantages and disadvantages of Stored Procedure?
Advantages:
A Stored Procedure can be used as a modular programming which means create once, store and
call for several times whenever it is required. This supports faster execution. It also reduces
network traffic and provides better security to the data.
Disadvantage:
The only disadvantage of Stored Procedure is that it can be executed only in the database and
utilizes more memory in the database server.
Q58. List all the types of user-defined functions?
There are three types of user-defined functions, namely:
 Scalar Functions
 Inline Table-valued functions
 Multi-statement valued functions
Scalar returns the unit, variant defined the return clause. Other two types of defined functions
return table.

Q59. What do you mean by Collation?


Collation is defined as a set of rules that determine how data can be sorted as well as compared.
Character data is sorted using the rules that define the correct character sequence along with
options for specifying case-sensitivity, character width etc.

Q60. What are the different types of Collation Sensitivity?


Following are the different types of collation sensitivity:
 Case Sensitivity: A and a and B and b.
 Kana Sensitivity: Japanese Kana characters.
 Width Sensitivity: Single byte character and double-byte character.
 Accent Sensitivity.
Apart from this SQL Interview Questions Blog, if you want to get trained from professionals on
this technology, you can opt for structured training from edureka! 

Q61. What are Local and Global variables?


Local variables:
These variables can be used or exist only inside the function. These variables are not used or
referred by any other function.
Global variables:
These variables are the variables which can be accessed throughout the program. Global
variables cannot be created whenever that function is called.

Q62. What is Auto Increment in SQL?


Autoincrement keyword allows the user to create a unique number to get generated whenever a
new record is inserted into the table.
This keyword is usually required whenever PRIMARY KEY in SQL is used.
AUTO INCREMENT keyword can be used in Oracle and IDENTITY keyword can be used in
SQL SERVER.
Q63. What is a Datawarehouse?
Datawarehouse refers to a central repository of data where the data is assembled from multiple
sources of information. Those data are consolidated, transformed and made available for the
mining as well as online processing. Warehouse data also have a subset of data called Data
Marts.

Q64. What are the different authentication modes in SQL Server? How can it be changed?
Windows mode and Mixed Mode – SQL and Windows. You can go to the below steps to change
authentication mode in SQL Server:
 Click Start> Programs> Microsoft SQL Server and click SQL Enterprise Manager to run
SQL Enterprise Manager from the Microsoft SQL Server program group.
 Then select the server from the Tools menu.
 Select SQL Server Configuration Properties, and choose the Security page.

Q65. What are STUFF and REPLACE function?


STUFF Function: This function is used to overwrite existing character or inserts a string into
another string. Syntax:
STUFF(string_expression,start, length, replacement_characters)
where,
string_expression: it is the string that will have characters substituted
start: This refers to the starting position
length: It refers to the number of characters in the string which are substituted.
replacement_string: They are the new characters which are injected in the string.
 
REPLACE function: This function is used to replace the existing characters of all the
occurrences. Syntax:
REPLACE (string_expression, search_string, replacement_string)
Here every search_string in the string_expression will be replaced with the replacement_string.

What are functions?


Functions are methods used to perform data operations. SQL has many in-built functions used to
perform string concatenations, mathematical calculations etc.
SQL functions are categorized into the following two categories:
1. Aggregate Functions
2. Scalar Functions
Let us look into each one of them, one by one.
Aggregate SQL Functions
The Aggregate Functions in SQL perform calculations on a group of values and then return a
single value. Following are a few of the most commonly used Aggregate Functions:
Function  Description
SUM() Used to return the sum of a group of values.
Returns the number of rows either based on a
COUNT() 
condition, or without a condition.
Used to calculate the average value of a
AVG()
numeric column.
This function returns the minimum value of a
MIN()
column. 
MAX() Returns a maximum value of a column. 
FIRST() Used to return the first value of the column.
This function returns the last value of the
LAST() 
column.
Let us look into each one of the above functions in depth. For your better understanding, I will be
considering the following table to explain to you all the examples.
StudentID StudentName Marks
1 Sanjay 64
2 Varun 72
3 Akash 45
4 Rohit 86
5 Anjali 92
SUM()
Used to return a total sum of numeric column which you choose.
Syntax:
1 SELECT SUM(ColumnName)
2 FROM TableName;
Example:
Write a query to retrieve the sum of marks of all students from the Students table.
1 SELECT SUM(Marks)
2 FROM Students;
Output:
1 359
COUNT() 
Returns the number of rows present in the table either based on some condition or without any
condition. Syntax:
1 SELECT COUNT(ColumnName)
2 FROM TableName
3 WHERE Condition;
Example:
Write a query to count the number of students from the Students table.
1 SELECT COUNT(StudentID)
2 FROM Students;
Output:
15
Example:
Write a query to count the number of students scoring marks > 75 from the Students table.
1 SELECT COUNT(StudentID)
2 FROM Students
3 WHERE Marks >75;
Output:
12
 AVG() 
This function is used to return the average value of a numeric column.
Syntax:
1 SELECT AVG(ColumnName)
2 FROM TableName;
Example:
Write a query to calculate the average marks of all students from the Students table.
1 SELECT AVG(Marks)
2 FROM Students;
Output:
1 71.8
MIN()
Used to return the minimum value of a numeric column.
Syntax:
1 SELECT MIN(ColumnName)
2 FROM TableName;
Example:
Write a query to retrieve the minimum marks out of all students from the Students table.
1 SELECT MIN(Marks)
2 FROM Students;
Output:

MAX()
Returns the maximum value of a numeric column.
Syntax:
1 SELECT MAX(ColumnName)
2 FROM TableName;
Example:
Write a query to retrieve the maximum marks out of all students from the Students table.
1 SELECT MAX(Marks)
2 FROM Students;
Output:

FIRST()
This function returns the first value of the column which you choose.
Syntax:
1 SELECT FIRST(ColumnName)
2 FROM TableName;
Example:
Write a query to retrieve the marks of the first student.

1 SELECT FIRST(Marks)
2 FROM Students;
Output:

LAST() 
Used to return the last value of the column which you choose.
Syntax:
1 SELECT LAST(ColumnName)
2 FROM TableName;
Example:
Write a query to retrieve the marks of the last student.
1 SELECT LAST(Marks)
2 FROM Students;
Output: 92
Well, with that we come to an end to SQL Aggregate Functions. Next in this article on SQL
Functions, let us understand the various Scalar Functions.
Scalar SQL Functions
The Scalar Functions in SQL are used to return a single value from the given input
value.  Following are a few of the most commonly used Aggregate Functions:
 Let us look into each one of the above functions in depth. 
Function Description
Used to convert string column values to
LCASE()
lowercase
This function is used to convert a string column
UCASE()
values to Uppercase.
Returns the length of the text values in the
LEN()
column.
Extracts substrings in SQL from column values
MID()
having String data type.
Rounds off a numeric value to the nearest
ROUND()
integer.
This function is used to return the current
NOW()
system date and time.
FORMAT() Used to format how a field must be displayed.
LCASE()
Used to convert values of a string column to lowercase characters.
Syntax:
1 SELECT LCASE(ColumnName)
2 FROM TableName;
Example:
Write a query to retrieve the names of all students in lowercase.
1 SELECT LCASE(StudentName)
2 FROM Students;
Output:
1 sanjay
2 varun
3 akash
4 rohit
5 anjali
UCASE()
Used to convert values of a string column to uppercase characters.
Syntax:
1 SELECT UCASE(ColumnName)
2 FROM TableName;
Example:
Write a query to retrieve the names of all students in lowercase.
1 SELECT UCASE(StudentName)
2 FROM Students;
Output:
1 SANJAY
2 VARUN
3 AKASH
4 ROHIT
5 ANJALI
LEN()
Used to retrieve the length of the input string.
Syntax:
1 SELECT LENGTH(String) AS SampleColumn;
Example:
Write a query to extract the length of the student name “Sanjay”.
1 SELECT LENGTH(“Sanjay”) AS StudentNameLen;
Output:
1 6
MID()
This function is used to extract substrings from columns having string data type.
Syntax:
1 SELECT MID(ColumnName, Start, Length)
2 FROM TableName;
Example:
Write a query to extract substrings from the StudentName column.
1 SELECT MID(StudentName, 2, 3)
2 FROM Students;
Output:
1 anj
2 aru
3 kas
4 ohi
5 nja
ROUND()
This function is used to round off a numeric value to the nearest integer.
Syntax:

1 SELECT ROUND(ColumnName, Decimals)


2 FROM TableName;

Example:
For this example, let us consider the following Marks table in the Students table.
StudentID StudentName Marks
1 Sanjay 90.76
2 Varun 80.45
3 Akash 54.32
4 Rohit 72.89
5 Anjali 67.66
Write a query to round the marks to the integer value. 
1 SELECT ROUND(Marks)
2 FROM Students;
Output:

1 91
2 80
3 54
4 73
5 68

NOW()
Used to return the current date and time. The date and time are returned in the “YYYY-MM-DD
HH-MM-SS” format.
Syntax:
1 SELECT NOW();
Example:
Write a query to retrieve the current date and time.
SELECT NOW();
Output:
NOW() 
2019-10-14 09:16:36

FORMAT()
This function formats the way a field must be displayed.
Syntax:
FORMAT(InputValue, Format)
Example:
Write a query to display the numbers “123456789” in the format “###-###-###”
SELECT FORMAT(123456789, “###-###-###”);
Output:
123-456-789

You might also like