SQL Flashcards

1
Q

How do you fetch common records from two tables?

A

Use the INTERSECT statement.

Select studentID from student
INTERSECT
Select StudentID from Exam

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

BETWEEN statement exclusivity.

A

BETWEEN is Inclusive for Date ranges and Exclusive for letter ranges

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to fetch Even or Odd rows from a table?

A

Use MOD statement

Even Rows
where mod(rowno,2)=0
Odd Rows
where mod(rowno,2)=1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to select unique records?

A

Use DISTINCT

Select DISTINCT EmailAddress From Blacklist

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to select substring of characters?

A

SUBSTRING function

Select SUBSTRING(StudentName,1,5) as studentname from student

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Pattern Matching command and syntax?

A

LIKE operator

SELECT Name FROM Student LIKE ‘%brad%’

% - Matches zero or more characters.
_(Underscore) – Matching exactly one character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between TRUNCATE and DROP statements?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you create an empty table from an existing table?

A

SELECT INTO NewTable statement

Select * into studentcopy from student where 1=2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an ALIAS command?

A

ALIAS name can be given to a table or column. This alias name can be referred in WHERE clause to identify the table or column

Select st.StudentID, Ex.Result from student st, Exam as Ex where st.studentID = Ex. StudentID

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you combine the result of two Select statements with the same schema structure without duplicates?

A

UNION Statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you get a result set that include all rows of the Left table while excluding any that are present in the Right table?

A

MINUS Statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is CLAUSE?

A

SQL clause is defined to limit the result set by providing condition to the query.

ex. WHERE, GROUP BY, ORDER BY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is collation?

A

Collation is defined as set of rules that determine how character data can be sorted and compared. This can be used to compare A and, other language characters and also depends on the width of the characters.

ASCII value can be used to compare these character data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What will be the result of the query below? Explain your answer and provide a version that behaves correctly.

select case when null = null then ‘Yup’ else ‘Nope’ end as Result;

A

This query will actually yield “Nope”, seeming to imply that null is not equal to itself! The reason for this is that the proper way to compare a value to null in SQL is with the is operator, not with =.

To fix
select case when null is null then ‘Yup’ else ‘Nope’ end as Result;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

LEFT JOIN

A

Statement that joins two tables on a condition, and keeps all rows of the LEFT table regardless of if the condition is met.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

RIGHT JOIN

A

Statement that joins two tables on a condition, and keeps all rows of the RIGHT table regardless of if the condition is met.

17
Q

INNER JOIN

A

Statement that joins two tables on a condition, and discards all rows for the tables that do not match the condition.

18
Q

OUTER JOIN

A

Statement that joins all the rows of two tables and any tuples that meet the condition join together.

19
Q

CROSS JOIN

A

Statement that joins all rows of LEFT table with all rows of the RIGHT table.

20
Q

How do you find a specific character in a string in SQL

A

MySQL - LOCATE function
LOCATE (‘ ‘, FullName)

SQL Server - CHARINDEX
CHARINDEX(‘ ‘, FullName) - 1)

21
Q

How to get a substring in SQL

A
MySQL - MID function
SELECT MID(FullName, 0, LOCATE(' ',FullName)) FROM EmployeeDetails
SQL Server - SUBSTRING function
SELECT SUBSTRING(FullName, 0, CHARINDEX(' ',FullName)) FROM EmployeeDetails
22
Q

If you have an Employee Table with the Schema
{id, name, manager_id, date_joined}

How do you find employees who are also managers?

A

You would use a self-join on the Employee table.

SELECT DISTINCT E.FullName
FROM EmpDetails E
INNER JOIN EmpDetails M
ON E.EmpID = M.ManagerID;

23
Q

How would you filter on a subquery where the result is not NULL

A

Using EXISTS

SELECT * FROM EmployeeDetails E 
WHERE EXISTS (SELECT * FROM EmployeeSalary S WHERE  E.EmpId = S.EmpId);
24
Q

How would you get duplicate records from a call?

A

Using GROUB BY + HAVING

SELECT EmpId, Project, Salary, COUNT()
FROM EmployeeSalary
GROUP BY EmpId, Project, Salary
HAVING COUNT(
) > 1;

25
Q

Getting current Datetime?

A

MySQL - NOW()
SQL Server - getdate()
Oracle - SELECT SYSDATE FROM DUAL;

26
Q

How to fetch the top N records?

A

MySQL - Limit
SELECT * FROM EmployeeSalary ORDER BY Salary DESC LIMIT N

SQLServer - SELECT TOP
SELECT TOP N * FROM EmployeeSalary ORDER BY Salary DESC

Oracle - ROWNUM
SELECT * FROM (SELECT * FROM EmployeeSalary ORDER BY Salary DESC)
WHERE ROWNUM <= 3;