SQL (TutorialsPoint) Flashcards

1
Q

What is the difference between SQL and MySQL or SQL Server?

A

SQL or Structured Query Language is a language; language that communicates with a relational database thus providing ways of manipulating and creating databases.

MySQL and Microsoft’s SQL Server both are relational database management systems that use SQL as their standard relational database language.

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

What is the difference between SQL and PL/SQL?

A

PL/SQL is a dialect of SQL that adds procedural features of programming languages in SQL. It was developed by Oracle Corporation in the early 90’s to enhance the capabilities of SQL.

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

What are various DDL commands in SQL? Give brief description of their purposes.

A

Following are various DDL or Data Definition Language commands in SQL −

CREATE − it creates a new table, a view of a table, or other object in database.

ALTER − it modifies an existing database object, such as a table.

DROP − it deletes an entire table, a view of a table or other object in the database.

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

What are various DML commands in SQL? Give brief description of their purposes.

A

Following are various DML or Data Manipulation Language commands in SQL −

SELECT − it retrieves certain records from one or more tables.

INSERT − it creates a record.

UPDATE − it modifies records.

DELETE − it deletes records.

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

What are various DCL commands in SQL? Give brief description of their purposes.

A

Following are various DCL or Data Control Language commands in SQL −

GRANT − it gives a privilege to user.

REVOKE − it takes back privileges granted from user.

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

Can you sort a column using a column alias?

A

Yes. A column alias could be used in the ORDER BY clause.

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

Is a NULL value same as zero or a blank space? If not then what is the difference?

A

A NULL value is not same as zero or a blank space. A NULL value is a value which is ‘unavailable, unassigned, unknown or not applicable’. Whereas, zero is a number and blank space is a character.

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

Say True or False. Give explanation if False.

If a column value taking part in an arithmetic expression is NULL, then the result obtained would be NULL.

A

True. When NULL enters the equation, we always end up with NULL

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

If a table contains duplicate rows, does a query result display the duplicate values by default? How can you eliminate duplicate rows from a query result?

A

A query result displays all rows including the duplicate rows. To eliminate duplicate rows in the result, the DISTINCT keyword is used in the SELECT clause.

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

What is the purpose of the condition operators BETWEEN and IN?

A

The BETWEEN operator displays rows based on a range of values. The IN condition operator checks for values contained in a specific set of values.

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

How do you search for a value in a database table when you don’t have the exact value to search for?

A

In such cases, the LIKE condition operator is used to select rows that match a character pattern. This is also called ‘wildcard’ search.

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

What is the default ordering of data using the ORDER BY clause? How could it be changed?

A

The default sorting order is ascending. It can be changed using the DESC keyword, after the column name in the ORDER BY clause.

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

What are the specific uses of SQL functions?

A

SQL functions have the following uses −

  1. Performing calculations on data
  2. Modifying individual data items
  3. Manipulating the output
  4. Formatting dates and numbers
  5. Converting data types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the case manipulation functions of SQL?

A

LOWER, UPPER, INITCAP

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

Which function returns the remainder in a division operation?

A

The MOD function returns the remainder in a division operation.

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

What is the purpose of the NVL function?

A

The NVL function converts a NULL value to an actual value.

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

What is the difference between the NVL and the NVL2 functions?

A
  1. The NVL(exp1, exp2) function converts the source expression (or value) exp1 to the target expression (or value) exp2, if exp1 contains NULL. The return value has the same data type as that of exp1.
  2. The NVL2(exp1, exp2, exp3) function checks the first expression exp1, if it is not null then, the second expression exp2 is returned. If the first expression exp1 is null, then the third expression exp3 is returned.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the use of the NULLIF function?

A

The NULLIF function compares two expressions. If they are equal, the function returns null. If they are not equal, the first expression is returned.

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

Discuss the syntax and use of the COALESCE function?

A

The COALESCE function has the expression COALESCE(exp1, exp2, …. expn)

It returns the first non-null expression given in the parameter list.

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

Which expressions or functions allow you to implement conditional processing in a SQL statement?

A

There are two ways to implement conditional processing or IF-THEN-ELSE logic in a SQL statement.

  1. Using CASE expression
  2. Using the DECODE function

SELECT cust_last_name

, CASE credit_limit

WHEN 100 THEN ‘Low’

WHEN 5000 THEN ‘High’

ELSE ‘Medium’

END

FROM customers;

SELECT product_id,

DECODE (warehouse_id

, 1, ‘Southlake’

, 2, ‘San Francisco’

, 3, ‘New Jersey’

, 4, ‘Seattle’

, ‘Non domestic’) “Location of inventory”

FROM inventories WHERE product_id < 1775;

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

You want to display a result query from joining two tables with 20 and 10 rows respectively. Erroneously you forget to write the WHERE clause. What would be the result?

A

The result would be the Cartesian product of two tables with 20 x 10 = 200 rows.

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

What is the difference between cross joins and natural joins?

A

The cross join produces the cross product or Cartesian product of two tables.

The natural join is based on all the columns having same name and data types in both the tables.

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

What is the purpose of the group functions in SQL? Give some examples of group functions.

A

Group functions in SQL work on sets of rows and returns one result per group. Examples of group functions are AVG, COUNT, MAX, MIN, STDDEV, SUM, VARIANCE.

24
Q

Say True or False. Give explanation if False.

By default the group functions consider only distinct values in the set.

A

By default, group functions consider all values including the duplicate values.

25
Q

Say True or False. Give explanation if False.

The DISTINCT keyword allows a function consider only non-duplicate values.

A

True.

26
Q

Say True or False. Give explanation if False.

All group functions ignore null values.

A

True.

27
Q

Say True or False. Give explanation if False.

COUNT(*) returns the number of columns in a table.

A

False. COUNT(*) returns the number of rows in a table.

28
Q

What’s wrong in the following query?

SELECT subject_code, count(name)

FROM students;

A

It doesn’t have a GROUP BY clause. The subject_code should be in the GROUP BY clause.

SELECT subject_code, count(name)

FROM students

GROUP BY subject_code;

29
Q

What’s wrong in the following query?
SELECT subject_code, AVG (marks) FROM students WHERE AVG(marks) > 75 GROUP BY subject_code;

A

The WHERE clause cannot be used to restrict groups. The HAVING clause should be used.

SELECT subject_code, AVG (marks)

FROM students

HAVING AVG(marks) > 75

GROUP BY subject_code;

30
Q

Say True or False. Give explanation if False.

Group functions cannot be nested.

A

False. Group functions can be nested to a depth of two.

Sample nested group function:

SELECT MAX(sal) FROM

(SELECT dept_id, SUM(salary) sal FROM employee

GROUP BY dept_id);

31
Q

What do you understand by a subquery? When is it used?

A

A subquery is a SELECT statement embedded in a clause of another SELECT statement.

It is used when the inner query, or the subquery returns a value that is used by the outer query.

It is very useful in selecting some rows in a table with a condition that depends on some data which is contained in the same table.

32
Q

Say True or False. Give explanation if False.

A single row subquery returns only one row from the outer SELECT statement

A

False. A single row subquery returns only one row from the inner SELECT statement.

33
Q

Say True or False. Give explanation if False.

A multiple row subquery returns more than one row from the inner SELECT statement.

A

True.

34
Q

Say True or False. Give explanation if False.

Multiple column subqueries return more than one column from the inner SELECT statement.

A

True.

35
Q

What’s wrong in the following query?

SELECT student_code, name

FROM students

WHERE marks = (SELECT MAX(marks) FROM students

GROUP BY subject_code);

A

Here a single row operator = is used with a multiple row subquery.

36
Q

What are the various multiple row comparison operators in SQL?

A

IN, ANY, ALL.

37
Q

What is the pupose of DML statements in SQL?

A

The DML statements are used to add new rows to a table, update or modify data in existing rows, or remove existing rows from a table.

38
Q

Which statement is used to add a new row in a database table?

A

The INSERT INTO statement.

39
Q

Say True or False. Give explanation if False.

While inserting new rows in a table you must list values in the default order of the columns.

A

True (assuming you don’t list the columns being set).

40
Q

How do you insert null values in a column while inserting data?

A

Null values can be inserted into a table by one of the following ways −

  1. Implicitly by omitting the column from the column list.
  2. Explicitly by specifying the NULL keyword in the VALUES clause.
41
Q

Say True or False. Give explanation if False.

INSERT statement does not allow copying rows from one table to another.

A

False. INSERT statement allows to add rows to a table copying rows from an existing table.

42
Q

How do you copy rows from one table to another?

A

The INSERT statement can be used to add rows to a table by copying from another table. In this case, a subquery is used in the place of the VALUES clause.

43
Q

What happens if you omit the WHERE clause in the UPDATE statement?

A

All the rows in the table are modified.

44
Q

Can you modify the rows in a table based on values from another table? Explain.

A

Yes. Use of subqueries in UPDATE statements allow you to update rows in a table based on values from another table.

45
Q

Say True or False. Give explanation if False.

The DELETE statement is used to delete a table from the database.

A

False. The DELETE statement is used for removing existing rows from a table.

The DROP statement is used to delete a table from the database.

46
Q

What happens if you omit the WHERE clause in a delete statement?

A

All the rows in the table are deleted.

47
Q

Can you remove rows from a table based on values from another table? Explain.

A

Yes, subqueries can be used to remove rows from a table based on values from another table.

48
Q

Say True or False. Give explanation if False.

Attempting to delete a record with a value attached to an integrity constraint, returns an error.

A

True.

FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table

49
Q

Say True or False. Give explanation if False.

You can use a subquery in an INSERT statement.

A

True.

50
Q

What is the purpose of the MERGE statement in SQL?

A

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

Example:

MERGE Production.UnitMeasure AS target

USING (SELECT @UnitMeasureCode, @Name) AS source (UnitMeasureCode, Name) ON (target.UnitMeasureCode = source.UnitMeasureCode)

WHEN MATCHED THEN UPDATE SET Name = source.Name

WHEN NOT MATCHED THEN INSERT (UnitMeasureCode, Name) VALUES (source.UnitMeasureCode, source.Name) OUTPUT deleted.*, $action, inserted.* INTO #MyTempTable;

51
Q

Say True or False. Give explanation if False.

A DDL statement or a DCL statement is automatically committed.

A

True.

52
Q

What is the difference between VARCHAR2 AND CHAR datatypes?

A

VARCHAR2 represents variable length character data, whereas CHAR represents fixed length character data.

53
Q

Say True or False. Give explanation if False.

A DROP TABLE statement can be rolled back.

A

False. A DROP TABLE statement cannot be rolled back.

54
Q

Which SQL statement is used to add, modify or drop columns in a database table?

A

The ALTER TABLE statement.

55
Q

What is a view? Why should you use a view?

A

A view is a logical snapshot based on a table or another view. It is used for −

  1. Restricting access to data;
  2. Making complex queries simple;
  3. Ensuring data independency;
  4. Providing different views of same data.
56
Q

Say True or False. Give explanation if False.

A view doesn’t have data of its own.

A

True.