4. Data Manipulation (DML) Flashcards

(11 cards)

1
Q

Insert 2 rows into the persons table, having columns: id (not null), first_name (not null), country and score.

A
INSERT INTO customers
(id, first_name, country, score)
VALUES 
(6, 'Sahil', 'India', NULL),
(7, 'Prince', NULL, 92)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Insert data from ‘customers’ table into ‘persons’ table.
persons: id (not null), person_name(not null), birth_date, phone (not null)
customers: id, first_name, country, score

A
insert into persons
select id, first_name, NULL, 'NA'
from customers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create table and insert data from another table in a single statement in Snowflake

A
CREATE TABLE region 
AS SELECT * FROM...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Change the score of customer with id 6 to zero.

A
update customers
set score = 0
where id = 6;

If we skip where clause, score of all customers will be changed to 0.

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

Change the country to UK and score to 0, for the customer with id 10.

A
update customers
set score=0, country='UK'
where id = 10;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

SQL command to Insert, update or delete values in customers table that are based on values in a staging_customers table or a subquery.

A

Merge

merge into customers as tgt
  using staging_customers as src
  on customers.id = staging_customers.id
  
  when matched and src.op = 'u' then
    update set
      tgt.name = src.name,
      tgt.email = src.email,
      tgt.updatedAt = CURRENT_DATE()
  
  when not matched and src.op='c' then
    insert (id, name, email, updatedAt)
    values (src.id, src.name, src.email, current_date())

Merging can be useful if the second table is a change log that contains new rows (to be inserted), modified rows (to be updated), or marked rows (to be deleted) in the target table.

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

You want to sync your inventory table with the latest stock levels from the warehouse system. If the product from warehouse_stock exists in inventory, update its quantity. If not, add it to inventory.

inventory: product_id, product_name, quantity.

warehouse_stock: product_id, product_name, quantity, updated_at.

A
merge into inventory as inv
using warehouse_stock as whs
on inv.product_id = whs.product_id
when matched then
update set inv.quantity = whs.quantity
when not matched then
insert (product_id, product_name, quantity)
values (whs.product_id, whs.product_name, whs.quantity);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Delete all customers with score 0.

A
delete from customers
where score = 0;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Truncate is faster than delete because _.

A

it skips checking and logging

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

delete all data from table persons.

A

truncate table persons;

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

In PostgreSQL,
USING lets you reference another table (customers) to decide which rows in orders to update/delete.

A
DELETE FROM target_table
USING other_table_or_subquery
WHERE target_table.col = other_table_or_subquery.col;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly