Test Your Knowledge: 25 SQL Subquery Questions

HOME




SELECT employee_id FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
   




SELECT * FROM employees WHERE department_id = (SELECT department_id FROM departments WHERE name = 'Sales');



a) SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
b) SELECT * FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id);
c) SELECT * FROM employees WHERE salary IN (SELECT salary FROM employees);
d) SELECT * FROM employees WHERE id IN (SELECT id FROM departments);



SELECT * FROM employees 
   WHERE salary BETWEEN (SELECT max(salary) FROM employees WHERE department_id = 100) AND 
  (SELECT min(salary) FROM employees WHERE department_id = 100);

SELECT department_id FROM departments WHERE department_id = (SELECT MAX(department_id) FROM departments);

a)  UPDATE employees SET salary = salary * 1.1 WHERE department_id = (SELECT department_id FROM departments);
b)	 UPDATE employees SET salary = salary * 1.1 WHERE department_id = (SELECT department_id FROM departments WHERE name = 'HR');
c)  UPDATE employees USING (SELECT department_id FROM departments WHERE name = 'HR');
d)  UPDATE employees SET salary = 1.1 * (SELECT salary FROM employees);

DELETE FROM employees WHERE manager_id IN (SELECT employee_id FROM employees WHERE department_id = 3);

SELECT first_name, last_name, salary FROM employees WHERE salary ANY 
(SELECT salary FROM employees);


a) SELECT * FROM (SELECT column1 FROM table1) AS subquery;
b) SELECT * FROM table1 WHERE column1 = (SELECT column2 FROM table2);
c) SELECT * FROM table1 INNER JOIN (SELECT column2 FROM table2) AS subquery;
d) SELECT column1 FROM table1 WHERE column2 = (SELECT column2 FROM table2) AND column3 = (SELECT column3 FROM table3);

SELECT first_name, last_name FROM employees WHERE emp_id  NOT IN 
(SELECT manager_id, hire_date FROM employees WHERE manager_id IS NOT NULL);





SQL Multiple Choice Questions & Answers – JOINS

HOME


SELECT products.product_name, orders.order_date
FROM products
INNER JOIN orders ON products.product_id = orders.product_id;


SELECT students.student_name, grades.grade
FROM students
__________ JOIN grades ON students.student_id = grades.student_id;
   


SELECT a.name, b.address
FROM customers a
FULL OUTER JOIN orders b ON a.customer_id = b.customer_id;


SELECT e.employee_id, e.name, d.department_name
FROM employees e
CROSS JOIN departments d;











What will be the result of inner join between these tables?



a) Select * FROM Table1 T1 CROSS JOIN Table1 T2;
b) Select * FROM Table1 T1 ALL CROSS JOIN Table1 T2;
c) Select * FROM Table1 T1,Table1 T2;
d) Select * FROM Table1 T1 CROSS Table1 T2;



SELECT * FROM students NATURAL JOIN enrollments;

Understanding SQL Injection: Types and Prevention

HOME

String username = request.getParameter("username");
String forename = request.getParameter("forename");

String sql = "SELECT * FROM users WHERE username = '" + username + "' AND forename = '" + forename + "'";

Connection conn = DriverManager.getConnection(url, username, forename);
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery(sql);
if (result.next()) {
    // User is authenticated
    String status = result.getString("success");
    System.out.println("Login to the application");
} else {
    // Authentication failed
  System.out.println("Unable to Login");
}

SELECT * FROM users WHERE username = 'admin'  AND forename = 'admin';

SELECT * FROM users WHERE username = 'admin'  -- AND forename = 'admin';

SELECT * FROM users WHERE username = 'admin'

String username = request.getParameter("username");
String forename = request.getParameter("forename");

String sql = "SELECT * FROM users WHERE username = ? AND forename = ?";

Connection conn = DriverManager.getConnection(url, username, forename);
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, forename);
ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
    // User is authenticated
       String status = result.getString("success");
       System.out.println("Login to the application");
} else {
    // Authentication failed
    System.out.println("Unable to Login");
}

SELECT ProductName, ProductDescription, ProductCost
FROM Products
WHERE ProductId = '100' UNION SELECT Username, Password FROM Users;

SQL Multiple Choice Answers – MCQ3

HOME

SQL Multiple Choice Questions – MCQ3

























SQL Multiple Choice Answers – MCQ2

HOME

SQL Multiple Choice Questions – MCQ2




















REVOKE INSERT, UPDATE ON Database.* FROM user123;





SQL Multiple Choice Answers – MCQ1

HOME

SQL Multiple Choice Questions – MCQ1


CREATE TABLE Students (ID int PRIMARY KEY, Name varchar(50));

SELECT column1, column2, ... FROM table_name;





UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

DELETE FROM table_name WHERE condition;



ALTER TABLE table_name
ADD column_name datatype;



a) SELECT FirstName FROM Students

a) SELECT * FROM Students

a) SELECT * FROM Students WHERE FirstName='Peter'

a) SELECT * FROM Students WHERE FirstName LIKE 'a%'


CREATE TABLE Students (ID int PRIMARY KEY, Name varchar(50));




SQL Multiple Choice Questions – MCQ1

HOME

Answer


Answer


Answer


Answer


Answer


Answer


DROP TABLE Student;

Answer


Answer


Answer


Answer


Answer


Answer


Answer


Answer


CREATE TABEL Students (ID int, Name varchar(50));

Answer


Answer


a) SELECT * FROM Students
b) SELECT [all] FROM Students
c) SELECT *.Students
d) SELECT Students

Answer


a) SELECT * FROM Students WHERE FirstName='Peter'
b) SELECT * FROM Students WHERE FirstName<>'Peter'
c) SELECT [all] FROM Students WHERE FirstName LIKE 'Peter'
d) SELECT [all] FROM Students WHERE FirstName='Peter'

Answer


a) SELECT * FROM Students WHERE FirstName LIKE 'a%'
b) SELECT * FROM Students WHERE FirstName='%a%'
c) SELECT * FROM Students WHERE FirstName='a'
d) SELECT * FROM Students WHERE FirstName LIKE '%a'

Answer


Answer


CREATE TABLE Students (ID int PRIMARY, Name varchar(50));

Answer


ALTER TABLE Students ADD COLUMN Email varchar(50);

Answer


Answer


SELECT Name FROM Employees WHERE Age > 30 AND Department = 'HR';

Answer

====================================================================

SQL Multiple Choice Questions – MCQ2
SQL Multiple Choice Questions – MCQ3

How to insert data in SQL Server using Java

HOME

jdbc:<driver protocol>:<driver connection details>
MS MySql Server - jdbc:odbc:DemoDSN
MySQL - jdbc:mysql://localhost:3306/demodb
Oracle - jdbc:orac;e:thin@myserver:1521:demodb
String dbUrl = "jdbc:mysql://localhost:3306/demo";
String username = "student";
String password = "student1$";

Connection conn = DriverManager.getConnection(dbUrl,username,password)
Statement stmt = conn.createStatement();

 int rowAffected = stmt.executeUpdate(
                    "insert into employees (last_name, first_name, email, department,salary) values ('Singh', 'Vibha','vibha.test@gmail.com', 'QA', 85000)");

<dependencies>
    <dependency>
      <groupId>com.mysql</groupId>
      <artifactId>mysql-connector-j</artifactId>
      <version>8.2.0</version>
</dependency>

package org.example;

import java.sql.*;

public class InsertRow_Demo {
    public static void main(String[] args) throws SQLException {

        Connection conn;
        Statement stmt = null;
        ResultSet result = null;
        ResultSet result1 = null;
        ResultSet result2 = null;

        String dbUrl = "jdbc:mysql://localhost:3306/demo";
        String username = "student";
        String password = "student1$";

        try {
            //Get a connection to database
            conn = DriverManager.getConnection(dbUrl, username, password);

            System.out.println("Database connection is successful\n");

            //Create a statement
            stmt = conn.createStatement();

            System.out.println("Inserting a new employee\n");

            int rowAffected = stmt.executeUpdate(
                    "insert into employees (last_name, first_name, email, department,salary) values ('Singh', 'Vibha','vibha.test@gmail.com', 'QA', 85000)");

            System.out.println("No of rows inserted :" + rowAffected);

            //Execute the SQL Query
            result = stmt.executeQuery("Select * from employees");

            //Process the result set
            while (result.next()) {
                System.out.println("First_Name :" + result.getString("first_name") + " , " + ("Last_Name :" + result.getString("last_name")));

            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Multiple Choice Questions

HOME

BDD Multiple Choice Questions – MCQ1

Cucumber Multiple Choice Questions – MCQ1
Cucumber Multiple-Choice Questions with Answers – MCQ2

Jenkins Multiple Choice Questions – MCQ1
Jenkins Multiple Choice Questions – MCQ2

GitHub Multiple Choice Questions – MCQ1

Docker – Basic Level – Multiple Choice Questions and Answers – MCQ1
Docker – Advance Level – Multiple Choice Questions and Answers – MCQ1

DW Testing – Basic Level – Multiple Choice Questions and Answers – MCQ1 – NEW