skip to content

In SQL, what exact GRANT gives an analyst read access to only the email and signup_date columns of a customers table?

level: middleimportance: should knowfreq 38%

answer

  1. the list sits beside the privilege
  2. parentheses come before ON
  3. what does SELECT star ask for
  4. a column revoke beside a table grant

basics

~20 s

GRANT SELECT (email, signup_date) ON customers TO analyst; puts the column list in parentheses right after the privilege. The analyst can select those columns by name, but SELECT * is refused because it asks for every column.

solid answer

~50 s

The column list goes directly after the privilege, before `ON`: `GRANT SELECT (email, signup_date) ON customers TO analyst;`. Each privilege carries its own list, so `GRANT SELECT (email), UPDATE (signup_date) ON customers TO analyst;` also works in PostgreSQL and MySQL. Queries must then stay inside the list. `SELECT email FROM customers` runs, while `SELECT *` is refused because it expands to every column, and a `WHERE country = 'DE'` is refused too because filtering reads `country`. In PostgreSQL only `SELECT`, `INSERT`, `UPDATE` and `REFERENCES` take a list, since `DELETE` removes whole rows. Oracle accepts lists on `INSERT`, `UPDATE` and `REFERENCES` but not `SELECT`. To remove access, write `REVOKE SELECT (email) ON customers FROM analyst;`. The trap is starting wide: PostgreSQL's reference warns that a table-level grant is unaffected by a later column revoke, so that column stays readable.

code

sql · 4 lines
sql
REVOKE SELECT ON customers FROM analyst;
GRANT SELECT (email, signup_date) ON customers TO analyst;

SELECT email, signup_date FROM customers;

go deeper

for a junior

Recall where the parentheses go: the column list follows the privilege and comes before ON. Write the analyst's statement out in full, then write the REVOKE with the same shape.

for a middle

Predict the edges of the grant: SELECT star is refused, a filter on an ungranted column is refused, and only some privileges take a list. Say why DELETE cannot be per column.

for a senior

Diagnose the classic failure where a table grant followed by a column revoke leaves the column readable. Give the fix in the right order and explain why that order matters.

for a principal

Column grants are hard to read in bulk and break every SELECT star query, so say when your team would accept that cost and how the decision gets recorded for later reviewers.

A **column-level privilege** is a privilege that applies to named columns of a table instead of the whole table. The request "an analyst reads two columns of `customers`" is one sentence in plain language and one statement in SQL, as long as the column list is in the right place. ## Where the column list goes The list belongs to the privilege, so it sits right after the privilege name and before `ON`: ```sql GRANT SELECT (email, signup_date) ON customers TO analyst; ``` Because the list belongs to each privilege, one statement can give different columns to different operations: ```sql GRANT SELECT (id, status), UPDATE (status) ON orders TO svc_orders; ``` That second form is what a narrow writer often needs: it reads `id` to find the row and changes only `status`. Both statements run on PostgreSQL. MySQL takes the same form, with its own account and object naming, such as `ON shop.customers TO 'analyst'@'%'`. ## What the analyst can now run Column privileges are checked against the columns a query actually touches, wherever they appear: - `SELECT email, signup_date FROM customers` runs, because both columns are covered. - `SELECT * FROM customers` is refused. The star expands to every column, and the analyst lacks most of them. The engine does not quietly return the permitted subset. - `SELECT email FROM customers WHERE country = 'DE'` is refused, because the filter reads `country`. The same goes for a column in `ORDER BY` or `GROUP BY`. This is the usual first complaint after such a grant. A dashboard built on `SELECT *` breaks, and the fix is to list the columns in the query, not to widen the grant. ## Which privileges take a list PostgreSQL's column form accepts four privileges: | Privilege | With a column list | |---|---| | `SELECT` | Read these columns | | `INSERT` | Supply values for these columns | | `UPDATE` | Change these columns | | `REFERENCES` | Point a foreign key at these columns | `DELETE` and `TRUNCATE` take no list because they remove whole rows; `GRANT DELETE (email) ON customers TO analyst;` is not a form the grammar has. `ALL PRIVILEGES (email)` is accepted in PostgreSQL and means those four privileges on that column. Engines differ here, and this one catches people. **Oracle accepts column lists on `INSERT`, `UPDATE` and `REFERENCES`, but not on `SELECT`**, so the analyst's request has no direct column grant there. SQL Server accepts the list after the object as well, as in `GRANT SELECT ON customers (email, signup_date) TO analyst;`. SQLite has no privilege statements at all. ## Taking it back, and the trap in that The reverse keeps the same shape: ```sql REVOKE SELECT (email) ON customers FROM analyst; ``` Two sentences in PostgreSQL's `REVOKE` reference decide how this behaves, and they point in opposite directions: 1. Revoking a privilege on a **table** also revokes the corresponding privileges on each of its columns. 2. Revoking a privilege on a **column** has no effect on a role that was granted that privilege on the table. The second one is the trap. A team grants `SELECT` on the table, then tries to carve out one sensitive column: ```sql GRANT SELECT ON customers TO analyst; REVOKE SELECT (ssn) ON customers FROM analyst; ``` No error appears and `analyst` can still read `ssn`. PostgreSQL's `GRANT` reference says that a user may read a column if they hold the privilege for either that column or its whole table, and that the table-level grant is unaffected by a column-level operation. To narrow it, revoke at the table level first, then grant the columns: ```sql REVOKE SELECT ON customers FROM analyst; GRANT SELECT (email, signup_date) ON customers TO analyst; ``` The order matters because of the first sentence above: run the other way round, the table revoke would wipe out the column grant you just made. ## What to take away - Build column access up from nothing. It cannot be reached by subtracting from a table grant. - Name the columns in every query the role runs, and test the grant by connecting as that role. A query outside the list fails at once, which makes this one of the cheaper statements to verify. - Whether a column grant or a restricted view is the better tool is a separate design question, and this statement does not settle it.

  • An analyst already holds SELECT on the whole customers table. What statements narrow that to two columns in PostgreSQL?
    Run `REVOKE SELECT ON customers FROM analyst;`, then `GRANT SELECT (email, signup_date) ON customers TO analyst;`. The order matters: a table-level revoke also clears column privileges, so the reverse order would remove the grant you just made. A column-level revoke alone does nothing while the table grant stands.
  • With GRANT SELECT (email) ON customers TO analyst in place, can analyst run SELECT email FROM customers WHERE country = 'DE'?
    No. Reading `country` in the filter needs `SELECT` on `country` as well, even though it is not in the output. On PostgreSQL and MySQL the query is refused until the column is added to the grant, for example `GRANT SELECT (country) ON customers TO analyst;`.
  • How do you let a service change only the status column of orders?
    Use `GRANT UPDATE (status) ON orders TO svc_orders;`. Setting any other column is refused. If its statements find the row with `WHERE id = ...`, add `SELECT (id)`, as in `GRANT SELECT (id), UPDATE (status) ON orders TO svc_orders;`, because the filter reads `id`.

  • Writes the column list after the table name and expects every engine to accept it.
  • Expects SELECT * to return only the columns the role was granted.
  • Grants SELECT on the table, then revokes one column to hide it.
  • Assumes a filter column needs no privilege because it is not in the output.
  • Expects a column list on DELETE, which always removes whole rows.
  • Assumes every engine allows a column list on SELECT, which Oracle does not.