Q1
True / FalseIn PostgreSQL, the INSERT statement is used to add new rows to an existing table.
The INSERT statement in PostgreSQL is used to add new rows of data into an existing table.
Q2
True / FalseThe syntax INSERT INTO table_name VALUES (value1, value2, ...); is used to insert a new row in PostgreSQL.
This is the correct syntax for inserting a new row into a table in PostgreSQL.
Q3
True / FalseYou can insert multiple rows into a PostgreSQL table with a single INSERT INTO statement.
PostgreSQL allows inserting multiple rows using a single INSERT INTO statement by separating the values with commas.
Q4
True / FalseIn PostgreSQL, you must always specify the column names when using the INSERT INTO statement.
Specifying column names is optional if you provide values for all columns in the correct order.
Q5
True / FalseThe RETURNING clause in PostgreSQL can be used with INSERT INTO to return the values of specified columns after the insertion.
The RETURNING clause allows you to return the values of specified columns after an INSERT operation.
Q6
True / FalseThe ON CONFLICT clause in PostgreSQL allows you to handle conflicts that arise when inserting data.
The ON CONFLICT clause can be used to specify alternative actions to take when a conflict occurs during an INSERT.
Q7
True / FalseIn PostgreSQL, the INSERT INTO ... SELECT statement can be used to insert data into a table based on a query.
The INSERT INTO ... SELECT statement allows you to insert data into a table based on the results of a query.
Q8
True / FalseYou cannot use a CTE (Common Table Expression) with an INSERT statement in PostgreSQL.
You can use a CTE (Common Table Expression) with an INSERT statement in PostgreSQL to organize complex queries.
Q9
True / FalseThe INSERT command in PostgreSQL supports the WITH clause for executing multiple INSERT operations in a single statement.
The WITH clause can be used to execute multiple INSERT operations in a single statement.
Q10
True / FalseIn PostgreSQL, the INSERT statement does not support conditional inserts using the WHERE clause.
The INSERT statement itself does not support the WHERE clause directly for conditional inserts. However, you can achieve conditional inserts by using subqueries or CASE statements within the VALUES clause.
Q11
Single ChoiceWhich of the following is the correct syntax to insert a new row into a table named employees with columns id, name, and salary in PostgreSQL?
The correct syntax for the INSERT statement in PostgreSQL includes specifying the columns and their corresponding values using the VALUES clause.
Q13
Single ChoiceHow do you insert multiple rows into a PostgreSQL table using a single INSERT statement?
PostgreSQL allows the insertion of multiple rows in a single INSERT statement by separating the value sets with commas
Q14
Single ChoiceWhat is the correct way to insert data into a table and return the inserted rows in PostgreSQL?
The RETURNING clause in PostgreSQL allows you to return the inserted rows immediately after the INSERT statement.
Q15
Single ChoiceWhich of the following SQL commands will insert a row into the departments table only if the row does not already exist, based on the department_id in PostgreSQL?
The ON CONFLICT DO NOTHING clause prevents insertion if the row already exists, based on the specified constraint.
Q18
Single ChoiceConsider a table products with a unique constraint on the product_code column. Which PostgreSQL statement will insert a new row, or update the existing row if the product_code already exists?
The ON CONFLICT clause with DO UPDATE is used to update the existing row if a conflict arises due to a unique constraint.
Q19
Single ChoiceHow can you insert a row into the orders table, ensuring that the order_id is automatically generated, but also include a custom value for it using PostgreSQL's INSERT command?
The DEFAULT keyword is used in PostgreSQL to automatically generate a value for the column, such as from a sequence or a default expression.
Q21
Multiple ChoiceWhich SQL command inserts a new row into the 'employees' table with all column values provided?
SQL Code
INSERT INTO employees (employee_id, first_name, last_name, department_id)
VALUES (1, 'John', 'Doe', 101);
This command inserts a new row into the 'employees' table, specifying values for all columns listed in the query.
Q22
Multiple ChoiceWhich SQL command inserts a new row with only some column values provided, using DEFAULT for others?
SQL Code
INSERT INTO orders (order_id, order_date, customer_id)
VALUES (1, CURRENT_DATE, DEFAULT);
This command inserts a new row into the 'orders' table, specifying values for 'order_id' and 'order_date', while using the default value for 'customer_id'.
Q23
Multiple ChoiceWhich SQL command inserts multiple rows into the 'products' table in a single query?
SQL Code
INSERT INTO products (product_id, product_name, price)
VALUES (1, 'Laptop', 999.99),
(2, 'Tablet', 499.99);
This command inserts two rows into the 'products' table with a single query, specifying values for 'product_id', 'product_name', and 'price'.
Q24
Multiple ChoiceWhich SQL command uses the RETURNING clause to get the inserted row's primary key value?
SQL Code
INSERT INTO customers (first_name, last_name)
VALUES ('Jane', 'Doe')
RETURNING customer_id;
This command inserts a new row into the 'customers' table and returns the 'customer_id' of the newly inserted row.
Q25
Multiple ChoiceWhich SQL command uses the ON CONFLICT clause to handle duplicate keys by doing nothing?
SQL Code
INSERT INTO users (user_id, username)
VALUES (1, 'jdoe')
ON CONFLICT (user_id) DO NOTHING;
This command attempts to insert a new row into the 'users' table but does nothing if a row with the same 'user_id' already exists.
Q26
Multiple ChoiceWhich SQL command inserts a new row into a table with a JSONB column and assigns default values to missing fields?
SQL Code
INSERT INTO settings (config_id, config_data)
VALUES (1, '{"theme": "dark"}')
ON CONFLICT (config_id)
DO UPDATE SET config_data = jsonb_set(settings.config_data, '{theme}', '"dark"', true);
This command inserts a row into the 'settings' table with a JSONB column. If a conflict occurs on 'config_id', it updates the 'theme' field to 'dark'.
Q27
Multiple ChoiceWhich SQL command inserts a row and returns the inserted row's entire data?
SQL Code
INSERT INTO employees (first_name, last_name, department_id)
VALUES ('Alice', 'Smith', 101)
RETURNING *;
This command inserts a new row into the 'employees' table and returns all columns of the newly inserted row.
Q28
Multiple ChoiceWhich SQL command performs an INSERT with a subquery that selects data from another table?
SQL Code
INSERT INTO sales (product_id, sale_date, quantity)
SELECT product_id, CURRENT_DATE, 1 FROM products WHERE product_name = 'Laptop';
This command inserts a new row into the 'sales' table by selecting data from the 'products' table, specifically for products named 'Laptop'.
Q29
Multiple ChoiceWhich SQL command uses the INSERT...ON CONFLICT clause to handle duplicate keys by updating the conflicting row?
SQL Code
INSERT INTO inventory (product_id, stock)
VALUES (1, 100)
ON CONFLICT (product_id)
DO UPDATE SET stock = EXCLUDED.stock + inventory.stock;
This command attempts to insert a new row into the 'inventory' table. If a conflict occurs on 'product_id', it updates the stock by adding the existing stock with the new value.
Q30
Multiple ChoiceWhich SQL command performs an INSERT with data returned from a CTE (Common Table Expression) in PostgreSQL?
SQL Code
WITH new_products AS (
SELECT product_id, product_name FROM products WHERE product_id > 100
)
INSERT INTO sales (product_id, sale_date)
SELECT product_id, CURRENT_DATE FROM new_products;
This command uses a CTE (new_products) to select data from the 'products' table and then inserts the selected data into the 'sales' table.