Sie sind auf Seite 1von 28

SQL Q&A

DDL
1.Query to create database-
create database dbname;
2.Query to delete the whole database-
DROP DATABASE dbname;
3.Query to truncate table from database-
TRUNCATE TABLE tablename;
After running the above query table will be truncated,
i.e, the data will be deleted but the structure will remain
in the memory for further operations.
DDL Contd.,
4.Differentiate drop and truncate-
 Truncate is normally ultra-fast and its ideal for
deleting data from a temporary table.
 Truncate preserves the structure of the table for future
use, unlike drop table where the table is deleted with
its full structure.
 Table or Database deletion using DROP statement
cannot be rolled back, so it must be used wisely.
DQL
1.Give a syntax for Group By clause using multiple
columns and demonstrate
SELECT column1, column2, Count(*) FROM table
name GROUP BY col
The table with both same column1 and column2 values
are placed in same group. And those whose only
column1 value is same but not that of column2 belongs
to different groups. So here we have grouped the table
according to two columns or more than one
column1,column2;
TCL
 Define TCL ;List commands
 Transaction Control Language

1. Commit
2. Rollback
3. SavePoint
4. Set Transaction
2.Differentiate WHERE clause and HAVING clause
 WHERE clause is used to place conditions on columns
but what if we want to place conditions on groups
 We can not use the aggregate functions like SUM(),
COUNT() etc. with WHERE clause. So we have to use
HAVING clause if we want to use any of these
functions in the conditions.
SQL DATATYPES
 Differentiate VARCHAR2 and VARCHAR datatype in
SQL?
Both of these datatypes are used for characters but
varchar2 is used for character strings of variable length
whereas varchar is used for character strings of fixed
length. For example, if we specify the type as
varchar(5) then we will not be allowed to store string
of any other length in this variable but if we specify the
type of this variable as varchar2(5) then we will be
allowed to store strings of variable length, we can store
a string of length 3 or 4 or 2 in this variable.
SQL OPERATORS
 Differentiate BETWEEN and IN operators in SQL?
BETWEEN
The BETWEEN operator is used to fetch rows based on a range of
values.
For example,
 SELECT * FROM Students WHERE ROLL_NO BETWEEN 20 AND 30;
This query will select all those rows from the table Students where the
value of the field ROLL_NO lies between 20 and 30.
IN
The IN operator is used to check for values contained in specific sets.
For example,
 SELECT * FROM Students WHERE ROLL_NO IN (20,21,23); This query
will select all those rows from the table Students where the value of the
field ROLL_NO is either 20 or 21 or 23.
SQL SUBQUERY
 Differentiate Co-related subquery and nested subquery
 Co-related sub query is one in which inner query is
evaluated only once and from that result outer query is
evaluated.Nested query is one in which Inner query is
evaluated for multiple times for getting one row of that
outer query.ex. Query used with IN() clause is Co-related
query. Query used with = operator is Nested query
 Co-Related Vs Nested-SubQueries. Technical difference
between Normal Sub-query and Co-related sub-query are:
1. Looping: Co-related sub-query loop under main-query;
whereas nested not;
SQL JOIN
 What is a Join in SQL?Explain different joins.,
 An SQL Join statement is used to combine data or rows from two or more tables
based on a common field between them. Different types of Joins are:
 INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as
long as the condition satisfies. This keyword will create the result-set by combining
all rows from both the tables where the condition satisfies i.e value of the common
field will be same.
 LEFT JOIN:This join returns all the rows of the table on the left side of the join and
matching rows for the table on the right side of join. The rows for which there is no
matching row on right side, the result-set will contain null. LEFT JOIN is also
known as LEFT OUTER JOIN
 RIGHT JOIN:RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of
the table on the right side of the join and matching rows for the table on the left
side of join. The rows for which there is no matching row on left side, the result-set
will contain null. RIGHT JOIN is also known as RIGHT OUTER JOIN.
 FULL JOIN: FULL JOIN creates the result-set by combining result of both LEFT
JOIN and RIGHT JOIN. The result-set will contain all the rows from both the tables.
The rows for which there is no matching, the result-set will contain NULL values.
SQL JOIN Contd.,
1.For the given tables Student and StudentCourse,
CARTESIAN JOIN
 Define cartesian join
The CARTESIAN JOIN is also known as CROSS JOIN.
In a CARTESIAN JOIN there is a join for each row of
one table to every row of another table. This usually
happens when the matching column or WHERE
condition is not specified.
 In the absence of a WHERE condition the CARTESIAN JOIN
will behave like a CARTESIAN PRODUCT . i.e., the number
of rows in the result-set is the product of the number of rows
of the two tables.
 In the presence of WHERE condition this JOIN will function
like a INNER JOIN.
 Generally speaking, Cross join is similar to an inner join
where the join-condition will always evaluate to True
CARTESIAN/CROSS JOIN Contd.,
 Give the output of the query from student and
studentcourse table which uses cross join
SELECT Student.NAME, Student.AGE,
StudentCourse.COURSE_ID FROM Student CROSS
JOIN StudentCourse;
OUTPUT OF THE QUERY-CARTESIAN JOIN
SELF JOIN
 Define Self join-
SELF JOIN a table is joined to itself. That is, each row
of the table is joined with itself and all other rows
depending on some conditions. In other words we can
say that it is a join between two copies of the same
table
SELF JOIN Contd.,
 Give the output of the query from student and
studentcourse table which uses cross join
SELECT a.ROLL_NO , b.NAME FROM Student a,
Student b WHERE a.ROLL_NO < b.ROLL_NO;
Output:
UNION AND UNION ALL
 What are union and union all clauses used for?
The Union clause produces distinct values in the
result set, to fetch the duplicate values too UNION
ALL must be used instead of just UNION.
FUNCTIONS
1.What function is used to extract texts from the text fields in
SQL.Give Syntax
MID() function is used.
Syntax:
SELECT MID(column_name,start,length) AS
some_name FROM table_name;
2.What function is used how to format a field is
displayed?Give Syntax
FORMAT() function is used.
Syntax:
SELECT FORMAT(column_name,format) FROM
table_name;
3.What is the function in sql to return current date and time give syntax
SELECT NOW();
4. What is the function in sql to return current date give syntax
SELECT CURDATE();
5. What is the function in sql to return current time give syntax
SELECT CURTIME();
6. What is the function in sql which extracts the date part of a date or date/time expression
SELECT DATE(dttmexprsnclmname) AS BirthDate FROM tablename;
7. What is the function in sql which returns a single part of a date or date/time expression
SELECT Extract(DAY FROM dttmexprsnclmname) AS BirthDay FROM tablename;
8. What is the function in sql which adds a specified time interval to a date expression
SELECT DATE_ADD(dttmexprsnclmname, INTERVAL 1 YEAR) AS BirthTimeModified
FROM tablename;
9.Use format function to format the current date and time field using SQL
SELECT NAME, FORMAT(Now(),'YYYY-MM-DD') AS Date FROM Students;
VIEWS
What are views ?What are the commands that can be used to
views?
 Views in SQL are kind of virtual tables. A view also has
rows and columns as they are in a real table in the
database. We can create a view by selecting fields from one
or more tables present in the database. A View can either
have all the rows of a table or specific rows based on certain
condition.
 CREATE VIEW
 SELECT
 DROP VIEW
 CREATE OR REPLACE VIEW statement to add or remove
fields from a view.
VIEWS contd.,
What are the conditions to be satisfied when updating a
view?
 The SELECT statement which is used to create the view
should not include GROUP BY clause or ORDER BY clause.
 The SELECT statement should not have the DISTINCT
keyword.
 The View should have all NOT NULL values.
 The view should not be created using nested queries or
complex queries.
 The view should be created from a single table. If the view
is created using multiple tables then we will not be allowed
to update the view.
VIEWS Contd.,
 Differentiate views and materialised views
Materialised views do not have the same rowid as that of
the actual table wherease views have the same rowid.
Materialised views are faster because indexing can be
applied since the result is stored in a db unlike view
which is a logical query executed everytime.
CONSTRAINTS
 Create a table named Student and specify the default
value for the field AGE as 18.
CREATE TABLE Student ( ID int(6) NOT NULL, NAME
varchar(10) NOT NULL, AGE int DEFAULT 18 );
The query uses default constraint to provide a default
value for the fields. That is, if at the time of entering
new records in the table if the user does not specify
any value for these fields then the default value will be
assigned to them.
QUIZ
Which of the following statements are TRUE about an SQL query?
 P : An SQL query can contain a HAVING clause even if it does
not have a GROUP BY clause
 Q : An SQL query can contain a HAVING clause only if it has a
GROUP BY clause
 R : All attributes used in the GROUP BY clause must appear in
the SELECT clause
 S : Not all attributes used in the GROUP BY clause need to
appear in the SELECT clause
A)P & Q
B)Q & R
C)P & R
D)R & S
The statement that is executed automatically by the
system as a side effect of the modification of the
database is
 Backup
 Assertion
 Recovery
 Trigger
SOME INTERESTING QUERIES FOR PRACTICE

 How to retrieve a second highest salary from a


table?(Hint: use Correlated query or TOP operator
and orderby clause)
 Write a query to split one column to multiple
columns(Hint: Use mid function)
 Get numeric values only using sql(Hint: use regular
expression)
 SELECT E1.salary FROM Employee E1 WHERE 1 = (SELECT
COUNT(1) FROM Employee E2 WHERE
E2.salary>E1.salary) [or]
 SELECT TOP 1 sal FROM (SELECT DISTINCT TOP 2 sal
FROM table_name ORDER BY sal ASC) AS
second_highest_salary ORDER BY sal
 SELECT MID(column_name,start,length) AS
column_name FROM table_name;
 SELECT REGEXP_REPLACE(AB12CD34EF,A|B|C|D|E|F, )
FROM DUAL;
[or]
 SELECT REGEXP_REPLACE(<Column Name>,[A-Za-z])
FROM <Table Name>;

Das könnte Ihnen auch gefallen