SQL MCQs- Which of the following is a legal expression in SQL?

Hello Coders! In this post, we will discuss SQL expressions. Before we move forward let us first understand what is SQL.

Introduction

What is SQL?

SQL basically stands for Structured Query Language. SQL lets you access and manipulates databases. It became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987.

Following things can be achieved by SQL-

  1. SQL can execute queries against a database.
  2. It can retrieve data from a database.
  3. It can insert/update/delete records in a database.
  4. SQL can create new databases.
  5. It can create views in a database.
  6. SQL can set permissions on tables, procedures, and views

The following image represents all commands in SQL.

SQL Commands: DML, DDL, DCL, TCL, DQL with Query Example

Some of The Most Important SQL Commands include:

  1. SELECT – extracts data from a database
  2. UPDATE – updates data in a database
  3. DELETE – deletes data from a database
  4. INSERT INTO – inserts new data into a database
  5. CREATE DATABASE – creates a new database
  6. ALTER DATABASE – modifies a database
  7. CREATE TABLE – creates a new table
  8. ALTER TABLE – modifies a table
  9. DROP TABLE – deletes a table
  10. CREATE INDEX – creates an index (search key)
  11. DROP INDEX – deletes an index

Let us now discuss some important SQL based MCQs

SQL MCQs

Q1. Which of the following is a legal expression in SQL?

A) SELECT NULL FROM EMPLOYEE;

B) SELECT NAME FROM EMPLOYEE;

C) SELECT NAME FROM EMPLOYEE WHERE SALARY=NULL;

D) None of the above

Answer: B. SELECT NAME FROM EMPLOYEE;

Explanation: The expression SELECT NAME FROM EMPLOYEE will fetch all the names of the employees from the EMPLOYEE table.

Q2. Which of the following is a valid SQL type?

A) CHARACTER

B) NUMERIC

C) FLOAT

D) All of the above

Answer: D. All of the above

Q3. Which command is used to select the distinct subject (SUB) from the table (BOOK)?

A) SELECT ALL FROM BOOK

B) SELECT DISTINCT SUB FROM BOOK

C) SELECT SUB FROM BOOK

D) All of the above

Answer: B. SELECT DISTINCT SUB FROM BOOK

Q4. How can you change “Thomas” into “Michel” in the “LastName” column in the Users table?

A. UPDATE User SET LastName = ‘Thomas’ INTO LastName = ‘Michel’

B. MODIFY Users SET LastName = ‘Michel’ WHERE LastName = ‘Thomas’

C. MODIFY Users SET LastName = ‘Thomas’ INTO LastName = ‘Michel’

D. UPDATE Users SET LastName = ‘Michel’ WHERE LastName = ‘Thomas’

Answer: D. UPDATE Users SET LastName = ‘Michel’ WHERE LastName = ‘Thomas’

Explanation: The UPDATE statement is used for modifying the table data by using the SET and WHERE clause. The SET clause is used to change the values of the column specified in the WHERE clause. syntax: UPDATE table SET column1 = expression1, column2 = expression2,… WHERE conditions

Q5. Which of the following is the correct order of a SQL statement?

A. SELECT, GROUP BY, WHERE, HAVING

B. SELECT, WHERE, GROUP BY, HAVING

C. SELECT, HAVING, WHERE, GROUP BY

D. SELECT, WHERE, HAVING, GROUP BY

Answer:  B. SELECT, WHERE, GROUP BY, HAVING

Explanation: In SQL statements, the WHERE clause always comes before GROUP BY, and the HAVING clause always comes after GROUP BY. Therefore, option B is the correct choice.

Q6. Find the cities name with the condition and temperature from table ‘whether’ where condition = sunny or cloudy but temperature >= 60.

A. SELECT city, temperature, condition FROM weather WHERE condition = ‘cloudy’ AND condition = ‘sunny’ OR temperature >= 60

B. SELECT city, temperature, condition FROM weather WHERE condition = ‘cloudy’ OR condition = ‘sunny’ OR temperature >= 60

C. SELECT city, temperature, condition FROM weather WHERE condition = ‘sunny’ OR condition = ‘cloudy’ AND temperature >= 60

D. SELECT city, temperature, condition FROM weather WHERE condition = ‘sunny’ AND condition = ‘cloudy’ AND temperature >= 60

Answer: C. SELECT city, temperature, condition FROM weather WHERE condition = ‘sunny’ OR condition = ‘cloudy’ AND temperature >= 60

Explanation: We know that the AND operator gives the output only when both the first conditions are true. In contrast, the OR operator gives the output when either the first condition OR the second condition is true. Hence the option C is the correct choice.

 Q7. Which of the following statement is correct to display all the cities with the condition, temperature, and humidity whose humidity is in the range of 60 to 75 from the ‘whether’ table?

A. SELECT * FROM weather WHERE humidity IN (60 to 75)

B. SELECT * FROM weather WHERE humidity BETWEEN 60 AND 75

C. SELECT * FROM weather WHERE humidity NOT IN (60 AND 75)

D. SELECT * FROM weather WHERE humidity NOT BETWEEN 60 AND 75

Answer: B. SELECT * FROM weather WHERE humidity BETWEEN 60 AND 75

Explanation: The BETWEEN is a conditional operator that is used to retrieve values from an expression within a range. It can be used with the SELECT, INSERT, UPDATE, and DELETE statement. The IN is a conditional operator used to reduce the use of multiple OR conditions in the SELECT, INSERT, UPDATE, and DELETE statement.

Hence the option B is the correct choice.

Conclusion

Hope you find this post helpful. We have discussed SQL MCQs. We now also hope that you will be able to identify the valid SQL expressions.

Stay tuned for more posts and updates. Until then, Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *