Sample Questions of OSS-DB Exam Silver "Development/SQL"

[S3.1 SQL commands]

3.59

Answer the result of executing "SELECT * FROM t1 ORDER BY id LIMIT 5 OFFSET 1;" on the following table.

postgres=# SELECT * FROM t1;
 id | name
----+------
  1 | aaa
  3 | ccc
  2 | bbb
  4 | ddd

A.  id | name
   ----+------
     1 | aaa
     3 | ccc
     2 | bbb
     4 | ddd

B.  id | name
   ----+------
     3 | ccc
     2 | bbb
     4 | ddd

C.  id | name
   ----+------
     2 | bbb
     3 | ccc
     4 | ddd

D. An error is output because the LIMIT value is greater than the total number of rows.


[S3.3 Transactions (LOCK statement)]

3.58

When executing 'LOCK TABLE t1;', choose the correct operation that other transactions are allowed to perform on table t1.

  1. SELECT statement

  2. INSERT statement

  3. UPDATE statement

  4. TRUNCATE statement

  5. Operations A-D are not allowed


[S3.1 SQL commands (Sequence)]

3.57

Choose the correct result for the currval command at the end of the following set of commands.

+--------------------------------------------------+
| Session 1 =# CREATE SEQUENCE test_seq;  |
| Session 1 =# SELECT nextval ('test_seq');   |
|  nextval                                                     |
| ---------                                                    |
|        1                                                       |
|  (1 row)                                                    |
| Session 2 =# SELECT currval ('test_seq');     |
+--------------------------------------------------+

  1. The result is 0.

  2. The result is 1.

  3. The result is 2.

  4. An error is output.


[S3.3 Transactions (Transaction isolation level (read committed, repeatable read, serializable))]

3.56

When executing the following series of transactions, choose the expected transaction isolation level for (tx2) from the following options: Read uncommitted, Read committed, Repeatable read, Serializable.

(tx1) BEGIN;
(tx2) BEGIN;
(tx1) SELECT * FROM test;
       id
      ----
        1
(tx1) INSERT INTO test VALUES (2);
(tx2) SELECT * FROM test;
       id
      ----
        1
(tx1) UPDATE test SET id=3 WHERE id = 1;
(tx2) SELECT * FROM test;
       id
      ----
        1
(tx1) COMMIT;
(tx2) SELECT * FROM test;
       id
      ----
        2
        3


[S3.1 SQL commands (Datatypes)]

3.55

Choose one incorrect description of the data type that handles JSON.

  1. The json and jsonb data types are available for storing JSON data.

  2. Because the json type simply stores a copy of the input value, multiple JSON objects may contain the same key and value.

  3. The jsonb type is much faster because it does not need to be reparsed each time it is processed.

  4. The json type supports indexes.

  5. The jsonpath type is provided to efficiently query the data that stores JSON.


[S3.1 SQL commands (Replication)]

3.54

Choose three appropriate descriptions for logical replication.

  1. It can replicate on a per-database basis.

  2. It can replicate on a per-table basis.

  3. All DDL commands are replicated.

  4. It can be used to replicate between different major versions.

  5. The tables to be replicated can be updated on the publisher side, but not on the subscriber side.


[S3.1 SQL commands (DELETE statement)]

3.53

You want to delete all data from the 'test' table and leave it empty. Choose two appropriate statements.

  1. Execute 'DELETE * FROM test;'.

  2. Execute 'DELETE ALL FROM test;'.

  3. Execute 'DELETE FROM test;'.

  4. Execute 'DROP ROWS FROM test;'.

  5. You can also execute 'TRUNCATE test;', which is faster.


[S3.1 SQL commands (UPDATE statement)]

3.52

You created the following tables, and created a row in each table:

create table dept(id integer, name varchar);
create table emp(id integer, name varchar, dept_id integer, dept_name text);

Now, to copy the 'name' column of the 'dept' table to the 'dept_name' column of the 'emp' table, you execute the following SQL:

update emp set dept_name = (select name from dept where id = emp.dept_id);

Choose the most appropriate statement. 

  1. The id column of the dept table must have a primary key (PRIMARY KEY) or unique key (UNIQUE) constraint.

  2. The dept_id column of the emp table must have a primary key (PRIMARY KEY) or unique key (UNIQUE) constraint.

  3. If there are duplicates in the id column of the dept table, an error may occur.

  4. If there are duplicates in the id column of the dept table, the result of update is unpredictable.

  5. Unable to execute due to syntax error.


[S3.1 SQL commands (INSERT statement)]

3.51

Choose the two most appropriate descriptions for an INSERT statement of the form:

INSERT INTO table_name(col1, col2...) VALUES (val1, val2...)

  1. If the table specified with table_name does not exist, it is automatically created.

  2. The column list specified with col1, col2 ... does not have to match the order of the columns in the actual table.

  3. Any columns in the table that are not included in the column list col1, col2 ... will be filled with NULL.

  4. If the number of data items in the value list specified with val1, val2 ... is larger than the number of columns in the column list specified with col1, col2 ..., then an error occurs.

  5. If the number of data items in the value list specified with val1, val2 ... is less than the number of columns in the column list specified with col1, col2 ..., then NULL is assumed for those columns.


[S3.1 SQL commands (SELECT statement)]

3.50

There is an emp table that records employee information and a dept table that records department name information.

=> select * from emp;
 emp_id | emp_name | dept_id
--------+----------+---------
      1 | Ichiro |       1
      2 | KIKUCHI Yusei |       1
      3 | MATSUI Hideki |       2
      4 | MATSUZAKA Daisuke |       3
(4 lines)

=> select * from dept;
 dept_id |   dept_name
---------+----------------
       1 | Mariners
       2 | Yankees
       3 | Red Sox
(3 lines)
 

You want to combine these tables to create a list that displays employee names and department names. What is the keyword that goes in the next SELECT statement?

select emp_id, emp_name, dept_name from emp join dept ____ emp.dept_id = dept.dept_id
 


  1. AT

  2. IN

  3. ON

  4. USING

  5. WITH


[S3.1 SQL commands (SELECT statement)]

3.49

In psql, a SELECT from the 'test' table produced the following output:

=> select * from test;
 a | b
---+---
 a | b
(1 line)

For this table, what does the output look like when you run 'select a b from test;'?


  1. a
    ---
    a

  2. a
    ---
    b

  3. b
    ---
    a

  4. b
    ---
    b

  5. None of the above.


[S3.3 Transactions (Transaction syntax)]

3.48

Choose three appropriate descriptions for SAVEPOINT.

  1. It is often executed with a savepoint name as a parameter, like SAVEPOINT name, but this parameter can be omitted.

  2. The savepoint name, specified as in SAVEPOINT name, must be unique within a transaction. Reusing the same name will result in an error.

  3. Executing ROLLBACK TO name will discard all updates made after the SAVEPOINT, while updates made before the savepoint are preserved.

  4. Executing ROLLBACK will discard all updates made within the transaction, including those made before the SAVEPOINT.

  5. Executing COMMIT will confirm all updates, both before and after the SAVEPOINT, and write them to the database.


[S3.2 Functions and operators (Date/Time functions)]

3.47

There is a table dt_sample created with the following SQL:

create table dt_sample(pk integer primary key, dt1 date, dt2 date, ti1 time, ts1 timestamp);

Choose three appropriate explanations for the results of the following operations on this table.

  1. Executing 'select dt1 + 10 from dt_sample;' will return the date that is 10 days after the date in dt1.

  2. Executing 'select dt2 - dt1 from dt_sample;' will return the number of days elapsed between dt1 and dt2 as an integer.

  3. Executing 'select ti1 + 100 from dt_sample;' will return the time that is 100 seconds after the time in ti1.

  4. Executing 'select ts1 + 10 from dt_sample;' will return the date and time that is 10 days after the timestamp in ts1.

  5. Executing 'select dt1 + ti1 from dt_sample;' will return a timestamp that combines the date from dt1 and the time from ti1.


[S3.2 Functions and operators (String functions)]

3.46

In the following descriptions of string functions, choose three that are appropriate.

  1. The character_length function can be used to obtain the byte length of a string.

  2. The substring function can retrieve a substring, such as the second to fifth characters of a string.

  3. The lower function can be used to determine if a string is made up of all lowercase letters.

  4. The trim function returns a string with whitespace removed from both ends of the string.

  5. The replace function can replace characters in a string, for example, changing all a's to A's.


[S3.2 Functions and operators (Aggregate functions)]

3.45

The following SQL statement was executed:

create table sample(val int);
insert into sample(val) values(null), (1), (2), (3), (4), (5);
select count(*), count(val), sum(val), avg(val), max(val) from sample;

Choose all correct statements from the following.


  1. The value of count(*) is 6.

  2. The value of count(val) is 6.

  3. The value of sum(val) is 15.

  4. The value of avg(val) is 2.5.

  5. The value of max(val) is NULL.


[S3.1 SQL commands (PL / pgSQL)]

3.44

A stored program was defined by executing the following:

create function sample_func(x integer) returns integer as $$
declare
begin
  raise info 'Hello World!';
  return x * 2;
end;
$$ language plpgsql;

Choose three appropriate descriptions for the defined program.


  1. To execute, you can use 'call sample_func(10)'.

  2. To execute, you can use 'select sample_func(10)'.

  3. When executed from a terminal, it displays 'Hello World!'.

  4. sample_func(10) returns a value of 20.

  5. When executed, an exception occurs and the command terminates abnormally.


[S3.1 SQL commands (Function definition / Procedure definition)]

3.43

The following commands were used to define tables and functions:

create table testa(id integer primary key, val varchar) ;
create function testfunc(x integer) returns varchar language sql as $$
select val from testa where id = x $$ ;

Choose the most appropriate privilege required to execute the following SELECT statement.

select testfunc(1);


  1. There are no particular restrictions, and any user can execute it.

  2. Only the owner of the function testfunc can execute it.

  3. This function can be executed if the EXECUTE privilege for the function testfunc has been GRANTED.

  4. It can be executed if the SELECT privilege on the table 'testa' has been GRANTED.

  5. Both the EXECUTE privilege for the function testfunc and the SELECT privilege on the table 'testa' must be GRANTED.


[S3.1 SQL commands (Partition)]

3.42

Select three potential benefits of partitioning a table.

  1. Improving access performance by reducing the disk capacity required by the table.

  2. Improving access performance by reducing the size of the index.

  3. Improving access performance by performing a full scan within a partition instead of an index scan.

  4. Reducing the processing time for operations such as bulk deletion of old data.

  5. Speeding up the process when an UPDATE causes data to move between partitions.


[S3.1 SQL commands (Tablespace)]

3.41

The tablespace ts1 was created with the CREATE TABLESPACE command. Choose two appropriate statements from the following descriptions.

  1. ts1 is available only from the database to which it was connected at the time of creation and cannot be used from any other database in the database cluster.

  2. To create a new object such as a table in ts1, you must have the appropriate privileges for ts1.

  3. To access the tables created in ts1, you must have the appropriate privileges for ts1.

  4. ts1 is dedicated to creating new objects only; you cannot move existing objects to ts1.

  5. For an existing table, only an index can be created on ts1.


[S3.1 SQL commands (Schema)]

3.40

Assume the result of 'show search_path' is as follows:


=> show search_path;
     search_path
-----------------
  "$user", public

Choose the two most appropriate descriptions for the execution of 'SELECT * FROM foo.bar'.


  1. It displays the contents of the table 'bar' in the database 'foo'.

  2. It displays the contents of the table 'bar' in the schema 'foo'.

  3. It displays the contents of the table 'bar' owned by the user 'foo'.

  4. Both the USAGE privilege on 'foo' and the SELECT privilege on 'bar' are required for execution.

  5. If executed by the user 'foo', 'SELECT * FROM bar;' will always yield the same result.


[S3.1 SQL commands (Sequence)]

3.39

A user A executed the following SQL:


create sequence testseq;
select nextval('testseq');

This SELECT statement returned 1. Choose three appropriate options for subsequent operations and their results. It is assumed that there are no issues with access permissions to this sequence and that no operations other than those described in each option have been performed.


  1. If the same user A continues to execute 'select currval('testseq');', it will return 1.

  2. If another user B connects to the database and executes 'select currval('testseq');', it will return 1.

  3. It returned 2 when another user C connected to the database and executed 'select nextval('testseq');'. Immediately after this, if user A executes 'select currval('testseq');', it will return 2.

  4. It returned 3 when another user D connected to the database and executed 'select nextval('testseq')' at exactly the same time as user C in option C. Immediately after this, if user C executes 'select currval('testseq');', it will return 2.

  5. If another user E connects to the database and sequentially executes 'select setval('testseq', 10); select nextval('testseq');', the second SELECT statement 'nextval' will return 11.


[S3.1 SQL commands (Materialized view)]

3.38

Choose one incorrect statement about views and materialized views.


  1. Materialized views can be indexed, but views cannot be indexed.

  2. Materialized views consume disk space depending on the amount of data, but views do not consume disk space even as data increases.

  3. Executing the same SELECT statement on a view and a materialized view with the same definition always returns the same result.

  4. When executing "SELECT * FROM view_name" on a view and a materialized view with the same definition, it is usually faster on the materialized view.

  5. Some views can be updated by INSERT/UPDATE/DELETE depending on their definition, but INSERT/UPDATE/DELETE operations cannot be performed on materialized views.


[S3.1 SQL commands (View)]

3.37

Choose two appropriate descriptions for a view.


  1. A SELECT statement can be executed on a view in the same way as on a table.

  2. Depending on the database version and the definition of the view, it may be possible to perform INSERT or UPDATE operations.

  3. An index can be created on a column in a view.

  4. If a table that serves as the basis for a view is dropped with DROP TABLE, the view becomes invalid.

  5. When dropping a view with DROP VIEW, specifying the CASCADE option allows the table that the view is based on to be dropped as well.


[S3.1 SQL commands (Index)]

3.36

The following commands were used to create tables x and y. Which columns are automatically indexed? Answer all that apply.



create table x (a integer primary key, b integer unique, c varchar(50) not null);
create table y (d integer primary key, e integer references x(b), f integer check (f > 0));

  1. Column a of table x

  2. Column b of table x

  3. Column c of table x

  4. Column d of table y

  5. Column e of table y

  6. Column f of table y


[S3.1 SQL commands (Table definition)]

3.35

Choose two correct syntaxes for creating a table 'test' with the column 'id' as the primary key.



  1. create table test (primary key id integer, val varchar(50));

  2. create table test (id integer primary key, val varchar(50));

  3. create table test (id integer, val varchar(50), constraint id primary key);

  4. create table test (id integer, val varchar(50), primary key (id));

  5. create table test (id integer, val varchar(50), primary key = id);


[S3.1 SQL commands (Data types)]

3.34

Choose two correct data types that can be used for tables in PostgreSQL.



  1. The 'integer' type is a 4-byte integer.

  2. The 'unsigned int' type can be used to handle non-negative integers.

  3. The 'long int' type can be used to handle 8-byte integers.

  4. The 'byte' type can be used to handle one-byte integers.

  5. The 'numeric' type can handle decimal numbers with more than 40 digits, including the decimal point.


[S3.1 SQL commands (DELETE statement)]

3.33

"What happens if the following command is executed?
DELETE FROM table_name;"



  1. An error occurs because the SQL syntax is wrong.

  2. Although it is recognized as a data deletion SQL command, nothing is deleted because there are no rows that match the WHERE clause.

  3. A prompt asking whether to delete the data is displayed.

  4. All rows in the table 'table_name' are deleted and the table becomes empty.

  5. The table 'table_name' is dropped.


[S3.1 SQL commands (UPDATE statement)]

3.32

The following sequence of SQL statements is executed. What is the value returned by the last sum?



 

create table sample (id integer primary key, val integer);
insert into sample(id) values (1), (2), (3), (4), (5);
update sample set val = id;
update sample set val = val * 2 where id > 2;
update sample set val = val + 1 where val < 4;
select sum(val) from sample;



[S3.1 SQL commands (INSERT statement)]

3.31

The following sequence of SQL commands is executed. What value does the final SELECT statement return as 'count'?



 

create table test(id integer primary key, val varchar);
insert into test(id, val) values(1, 'aaa'), (2, 'bbb'), (3, 'ccc');
insert into test(id, val) values(2, 'ddd'), (4, 'eee');
insert into test(id, val) values(3, 'fff'), (6, 'ggg');
insert into test(id, val) values(4, 'hhh'), (8, 'iii');
select count(*) from test;



[S3.1 SQL commands (SELECT statement)]

3.30

The following is a part of the sales table that records the sales date (sales_date), sales person (sales_person), client (client), item name (item), sales amount (amount), and so on.



 

select * from sales;
 sales_id |  sales_date  | sales_person|  client  |   item        | amount
---------+-------------+-------------+--------+-------------+----------
          1 | 2021-04-21 | Maeda           |  A Store | Computer | 100000
          2 | 2021-04-22 | Ohtani           |  B Shop | Printer     |   20000
          3 | 2021-04-23 | Arihara          |  C Plant | Router      |   10000
          4 | 2021-04-24 | Maeda           |  D Store | Mouse      |     2000  

You want to list the sales persons whose total sales amount is greater than 100000. In the following SELECT statement, what keywords should be filled in the blanks x, y?

select sales_person from sales [x] sales_person [y] sum(amount) > 100000;
 


  1. GROUP BY

  2. HAVING

  3. JOIN

  4. ORDER BY

  5. WHERE


[S3.1 SQL commands (SELECT statement)]

3.29

There are two tables: the 'dept' table, which stores the department ID and department name, and the 'emp' table, which stores the employee ID, employee name, date of birth, and department ID. Each has the following structure:


 

=> \d dept
      Table "public.dept"
  Columns  |       Type     | Collation | Null allowed | Default
-------------+-------------------+----------+---------------+---------
dept_id      | integer               |             | not null        |            
dept_name | character varying |             |                   |
Index:
    "dept_pkey" PRIMARY KEY, btree (dept_id)

=> \d emp
      Table "public.emp"
  Columns |   Type          | Collation | Null allowed | Default
-------------+--------------------+----------+--------------+---------
emp_id       | integer               |             | not null        |            
emp_name  | character varying |             |                   |
dob            | date                   |             |                   |
dept_id       | integer               |             |                   |
Index:
    "emp_pkey" PRIMARY KEY, btree (emp_id)

 

Choose three appropriate SELECT statements to join these tables and display a list of employees with their department names.


  1. select * from emp, dept join dept_id;

  2. select * from emp e, dept d where e.dept_id = d.dept_id;

  3. select * from emp natural join dept;

  4. select * from emp join dept using (dept_id);

  5. select * from emp e join dept d where e.dept_id = d.dept_id;


[S3.1 SQL commands (SELECT statement)]

3.28

When you use psql and run 'select current_timestamp;' to check the current time, the result is:

      current_timestamp
-------------------------------
2021-02-26 22:31:02.311235+09
(1 line)

However, when you run 'select current_timestamp from x;', the result is:

current_timestamp
-------------------
(0 lines)
 

Choose the most appropriate reason why the current time is not displayed when 'from x' is specified.


  1. Table x does not exist.

  2. You do not have SELECT privilege on table x.

  3. Table x does not have a current_timestamp column.

  4. Table x exists but contains no rows of data.

  5. You should not specify a from clause when retrieving current_timestamp.


[S3.3 Transactions (Deadlock)]

3.27

Choose three appropriate strategies to avoid deadlocks.

  1. Ensure each transaction is completed as quickly as possible.

  2. Once a lock is acquired, hold it for as long as possible.

  3. Aim to execute as many transactions as possible simultaneously.

  4. When locking multiple resources, maintain the same order of acquiring locks for each transaction.

  5. Avoid acquiring unnecessary locks.


[S3.3 Transactions (Transaction isolation level)]

3.26

Choose one incorrect statement about PostgreSQL's transaction isolation level.

  1. You can specify the transaction isolation level by using 'SET TRANSACTION ISOLATION LEVEL xxx' at the start of the transaction.

  2. You can specify one of four transaction isolation levels: SERIALIZABLE, REPEATABLE READ, READ COMMITTED, or READ UNCOMMITTED.

  3. The default transaction isolation level is specified in the configuration parameters; if not specified, REPEATABLE READ is the default.

  4. REPEATABLE READ is an isolation level that prevents non-repeatable reads and also eliminates phantom reads.

  5. READ UNCOMMITTED is an isolation level that allows reading of uncommitted data, but does not result in dirty reads.


[S3.3 Transactions (Transaction syntax)]

3.25

The following sequence of SQL statements was executed. What is the value returned by the last SELECT statement?

create table test(id integer);
begin;
insert into test(id) values(1);
commit;
begin;
insert into test(id) values(2);
savepoint x;
insert into test(id) values(3);
rollback to x;
insert into test(id) values(4);
commit;
begin;
insert into test(id) values(5);
savepoint x;
insert into test(id) values(6);
rollback;
commit;
select sum(id) from test;


[S3.2 Functions and operators (String operators)]

3.24

Choose three appropriate descriptions of string handling in PostgreSQL.


  1. You can use || to concatenate strings. For example, 'abc' || 'xyz' becomes 'abcxyz'.

  2. You can use + to concatenate strings. For example, 'abc' + 'xyz' becomes 'abcxyz'.

  3. LIKE, which is used for string comparison, is case-insensitive. Therefore, 'ABC' LIKE 'abc' is true.

  4. In string comparison with LIKE, _ and % are wildcards. For example, 'abcde' LIKE 'a_c%' is true.

  5. SIMILAR TO, which is used for comparison with regular expressions, supports wildcards _ and %, unlike POSIX regular expressions. For example, 'abcde' SIMILAR TO 'a_c%' is true.


[S3.2 Functions and operators (String functions)]

3.23

Choose one incorrect description for a function that handles strings.


  1. character_length is a function that returns the length of a string in characters. For example, character_length('josé') returns 4.

  2. byte_length is a function that returns the length of a string in bytes. For example, in a database with a UTF-8 character set, byte_length('josé') returns 5.

  3. substring is a function that returns a substring. For example, substring('12345678' from 3 for 2) returns '34'.

  4. replace is a function that replaces strings. For example, replace('12341234', '2', 'x') returns '1x341x34'.

  5. trim is a function that removes whitespace from both ends of a string. For example, trim(' abc ') returns 'abc'.


[S3.2 Functions and operators (Operators)]

3.22

Choose one appropriate statement about an operator to use in PostgreSQL.


  1. You can use // to obtain the integer quotient by discarding the remainder in division. For example, 5 // 3 results in 1.

  2. You can use % to find the remainder of the division. For example, 5 % 3 results in 2.

  3. ** can be used to calculate the power. For example, 5 ** 3 results in 125.

  4. You can use || to determine the logical OR. For example, true || false is true.

  5. You can use + to concatenate strings. For example, 'abc' + 'def' results in 'abcdef'.


[S3.2 Functions and operators (Mathematical functions)]

3.21

Choose three correct descriptions of PostgreSQL functions.


  1. Use the rounddown function to truncate a decimal part to an integer.

  2. Use the trunc function to truncate the decimal part to an integer.

  3. Use the roundup function to round up a decimal part to an integer.

  4. Use the ceil function to round up a decimal part to an integer.

  5. Use the round function to round the decimal part to an integer.

  6. Use the int function to round a decimal part to an integer.


[S3.2 Functions and operators (Aggregate functions)]

3.20

Choose one appropriate statement about the behavior
when you execute:

SELECT count(*), count(x) FROM sample_table;


  1. Both count(*) and count(x) return the number of rows in the sample_table.

  2. count(*) returns the number of rows in sample_table, and count(x) returns the number of different types of values in column x.
  3. count(*) returns the number of rows in sample_table, and count(x) returns the number of rows where the value in column x is not NULL.
  4. count(*) returns the number of rows in sample_table where no column is NULL, and count(x) returns the number of rows where the value in column x is not NULL.
  5. count(*) returns the sum of the values in the leftmost column of sample_table, and count(x) returns the sum of the values in column x.

[S3.1 SQL commands (PL / pgSQL)]

3.19

We want to use PL/pgSQL to define a function like this:

create function test_func(a integer) returns integer as $$
declare
x record;
begin
select * into x from test_table
where id = a;
return x.value;
end;
$$ language plpgsql;

Choose an appropriate explanation for this.


  1. The create function fails because a semicolon is required after declare on the second line.

  2. The create function fails because the x record on line 3 is incorrect. The correct value is x test_table%ROWTYPE.
  3. The create function fails because a semicolon is required after begin on line 4.
  4. If the test_table referenced on line 5 does not exist, create function fails.
  5. If the id column referenced on line 6 does not exist, the create function succeeds, but a run-time error occurs when the function is called.

[S3.1 SQL commands (Function definition / Procedure definition)]

3.18

CREATE FUNCTION func_add(x integer, y integer) RETURNS integer AS $$
SELECT x + y;
$$ LANGUAGE SQL;

Choose three appropriate explanations for the above.


  1. PL/pgSQL is used to define a function.

  2. It defines a function called func_add.
  3. SELECT func_add(2, 3) returns 5.
  4. ' (single quote) may replace $$ at the end of line 1 and the beginning of line 3.
  5. CREATE FUNCTION requires administrator privileges on the database.

[S3.1 SQL commands (Partition)]

3.17

Choose the correct two steps for creating a partitioned table (declarative partitioning introduced in PostgreSQL version 10) and its partitions.

  1. Create a partitioned table using the CREATE PARTITIONED TABLE command.

  2. Create a partitioned table by specifying the PARTITIONED BY option in the CREATE TABLE command.
  3. Create partitions with the CREATE PARTITION command.
  4. Create a partition with the CREATE TABLE PARTITION command.
  5. Create a partition by specifying the PARTITION OF option in the CREATE TABLE command.

[S3.1 SQL commands (Tablespace)]

3.16

Choose one incorrect use of the tablespace.

  1. A directory to be used as a tablespace is specified in the CREATE TABLESPACE command, but the directory must already exist and be empty.

  2. If a table is created by executing 'CREATE TABLE (... table_definition ...) TABLESPACE tablespace_name;', then the specified tablespace is used. 
  3. If you specify a tablespace name with the default_tablespace parameter, objects created such as by CREATE TABLE use the specified tablespace.
  4. To create objects in a tablespace, you must have CREATE privilege on the tablespace.
  5. When a tablespace is dropped with the DROP TABLESPACE command, the objects in it are also automatically dropped.

[S3.1 SQL commands (Schema)]

3.15

The schema search path for user foo is set to “$user”, public by default.
The public schema has a table called bar, and all users (public) have been GRANTed privileges such as SELECT.
The schema foo exists, but there are no tables in it.
At this time, select all the correct statements from the following descriptions.

  1. SELECT * FROM bar; will result in an error as it fails to find the table foo.bar.

  2. CREATE TABLE bar(...); will result in an error because the table public.bar already exists.

     At this point, you execute CREATE TABLE foo.bar(...); to create two tables, foo.bar and public.bar.
     
  3. When you execute SELECT * FROM bar; it returns the data retrieved from both tables, foo.bar and public.bar.
  4. Executing DROP TABLE bar; will only remove the table foo.bar.
  5. Executing DROP TABLE bar; will only delete the table public.bar.

[S3.1 SQL commands (Sequence)]

3.14

The following sequence of operations was performed. What value does the last SELECT statement return as currval?

create table test1(id integer, val varchar(10));
create table test2(id integer, val varchar(10));
create sequence seq1;
insert into test1(id, val) values (1, 'abc'), (2, 'def');
insert into test2(id, val) values (nextval('seq1'), 'xyz');
begin;
insert into test2(id, val) select nextval('seq1'), val from test1;
rollback;
insert into test2(id, val) values (nextval('seq1'), 'pqr');
select currval('seq1');


[S3.1 SQL commands (Trigger)]

3.13

You defined statement-level BEFORE triggers, statement-level AFTER triggers, row-level BEFORE triggers, and row-level AFTER triggers for an UPDATE of a table. What is the order in which triggers execute when an UPDATE is performed on this table?

  1. BEFORE statement -> BEFORE row -> AFTER statement -> AFTER row

  2. BEFORE statement -> BEFORE row -> AFTER row -> AFTER statement
  3. BEFORE row -> BEFORE statement -> AFTER row -> AFTER statement
  4. BEFORE row -> BEFORE statement -> AFTER statement -> AFTER row
  5. BEFORE -> AFTER, but the order of execution at the statement and row levels is determined by the alphabetical order of the function name that defines the trigger.

[S3.1 SQL commands (View)]

3.12

Choose two things you can do with the ALTER VIEW command.

  1. Add a column to a view.

  2. Remove a column from a view.
  3. Change the data type of a column in a view.
  4. Rename a view.
  5. Change the owner of a view.

[S3.1 SQL commands (Index)]

3.11

When is an index automatically created when CREATE TABLE is executed? Choose all that apply.

  1. When a table has a FOREIGN KEY constraint

  2. When a NOT NULL constraint is applied to a column
  3. When a PRIMARY KEY constraint is applied to a column
  4. When a REFERENCES constraint is applied to a column
  5. When a UNIQUE constraint is applied to a column

[S3.1 SQL commands (Table definition)]

3.10

Choose one that cannot be changed by ALTER TABLE.

  1. Name of the table

  2. Owner of the table
  3. Name of the column
  4. Data type of the column
  5. Order of the columns

[S3.1 SQL commands (Data types)]

3.09

Choose one incorrect description of the data types available in PostgreSQL.

  1. integer is a 4-byte integer that can safely contain up to 10 digits.

  2. real and double precision are floating point real numbers, and double precision can perform more accurate operations.

  3. numeric is a numeric type that deals with integers or fractional numbers. Its precision can be defined by specifying the total number of digits and the number of digits after the decimal point.
  4. date is a data type that handles dates, and it does not hold time information.
  5. timestamp is a data type that handles dates and times, and it holds time data in microseconds.

[S3.1 SQL commands (Partition)]

3.08

Choose 3 appropriate descriptions of declarative partitioning supported from version 10 of PostgreSQL and partitioning using inheritance supported from previous versions. Assume that the parent table is named parent and the partitions are named partition1, partition2, partition3 ....

  1. In either method, the parent table is created using the usual CREATE TABLE parent... and no special options are needed to use partitions.

  2. When creating a partition, the command is
    CREATE TABLE partition1 PARTITION OF parent...
    for declarative partitioning, and
    CREATE TABLE partition1 () INHERITS (parent)
    for inheritance, both using CREATE TABLE.

  3. In either method, partitions that will store the data are not automatically created when executing the INSERT statement, and partitions need to be created in advance.
  4. In either method, if the parent table and partitions are correctly created with the CREATE command,
    INSERT INTO parent...
    will automatically store the inserted data into the appropriate partition.
  5. In declarative partitioning, all partitions have the same columns and different columns cannot be added, but in partitioning using inheritance, different columns can be added for each partition.

[S3.1 SQL commands (Materialized view)]

3.07

You created a materialized view mview that combines tables table1 and table2. Choose three appropriate statements.

  1. The command used to create it is in the format of
    CREATE MATERIALIZED VIEW mview AS SELECT ...  FROM table1, table2...

  2. Rather than executing the SELECT statement used in the AS clause during creation,
    SELECT * FROM mview
    is usually faster.

  3. The rows included in the result of executing the SELECT statement used in the AS clause during creation and the result of executing
    SELECT * FROM mview
    are always the same.
  4. You updated a row in table1 that is the source of mview, but the SELECT from mview showed the old data prior to the update.
  5. Although the size of the source table table2 is huge, only the definition statement of mview is saved in the database. Therefore, you do not have to worry about running out of disk space when creating a materialized view.

[S3.1 SQL commands (Procedure definition)]

3.06

Choose two appropriate explanations for the differences between the procedures (PROCEDURE) added in PostgreSQL version 11 and the functions (FUNCTION) supported from previous versions.

  1. A PROCEDURE is created with the CREATE PROCEDURE command, and a FUNCTION is created with the CREATE FUNCTION command.

  2. While PROCEDURE is defined in the SQL standard, FUNCTION is a PostgreSQL-specific extension.

  3. FUNCTION of type void is being deprecated and it is recommended to replace it with PROCEDURE.
  4. A FUNCTION is executed as SELECT func_name(arg1, arg2 ...), whereas a PROCEDURE is executed as CALL proc_name(arg1, arg2 ...).
  5. While a PROCEDURE can update data in the database, a FUNCTION can only perform calculations based on the data and cannot update the data.

[S3.2 Functions and operators (Date/Time functions)]

3.05

Select statement for a function that returns the current time, current_timestamp, was executed twice in succession as follows:

select current_timestamp;
select current_timestamp;

Both times, the same value was returned. Choose the most appropriate reason for this.

  1. The server machine's hardware clock is faulty.

  2. The timed command is not executed on the server machine, so the time is not accurately maintained.

  3. The same time was returned because the two SELECT statements were executed quickly in succession, without any gap.
  4. The BEGIN command was executed just before these SELECT statements.
  5. When executed from the same session, current_timestamp always returns the same value.

[S3.1 SQL commands (Partition)]

3.04

Choose all the potential benefits of using table partitioning.

  1. Reduce the disk space occupied by data.

  2. Significantly improve the performance of queries.

  3. Allocate frequently accessed rows and less frequently accessed rows to different disk spaces.
  4. Allocate frequently accessed columns and less frequently accessed columns to different disk spaces.
  5. Execute DROP TABLE instead of DELETE to speed up the bulk deletion of large amounts of data.

[S3.1 SQL commands (Tablespace)]

3.03

Choose three correct ways to use tablespaces.

  1. Place the new database created by CREATE DATABASE in a tablespace different from the default one.

  2. Place the new schema created by CREATE SCHEMA in a tablespace different from the default one.
  3. Place the new table created by CREATE TABLE in a tablespace different from the default one.
  4. Place the new view created by CREATE VIEW in a tablespace different from the source table.
  5. Place the new index created by CREATE INDEX in a tablespace different from the source table.

[S3.1 SQL commands (DELETE statement)]

3.02

Choose the two correct descriptions for materialized views and regular views.

  1. Both are created with the CREATE VIEW command.

  2. If the view definitions are the same, searching from materialized views is generally faster.
  3. If the view definitions are the same, a SELECT from either will always return the same result.

  4. Materialized views require disk space according to the amount of data they contain, while regular views do not require disk space.

  5. To modify a query that defines a view, the CREATE OR REPLACE VIEW command is used for a regular view, whereas a special command called REFRESH MATERIALIZED VIEW is used for a materialized view. 


[S3.1 SQL commands (DELETE statement)]

3.01

The primary key column of table xxx is an integer type id, and the primary key column of table yyy is also an integer type id.
If both xxx and yyy have rows with the same id, you want to delete all those rows from xxx.
For example, if 'select id from xxx' returns 1, 2, and 3, and 'select id from yyy' returns 2, 3, and 4, you want to delete the rows with id 2 and 3 from xxx. Which SQL statement accomplishes this?

  1. DELETE FROM xxx, yyy WHERE xxx.id = yyy.id;

  2. DELETE FROM xxx, yyy WHERE xxx.id IN (yyy.id);
  3. DELETE FROM xxx JOIN yyy USING xxx.id = yyy.id;

  4. DELETE FROM xxx WHERE id IN (SELECT id FROM yyy);

  5. DELETE FROM xxx WHERE EXISTS (SELECT id FROM yyy);