Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Primary Key Column Check
00:00
5 left

Primary Key Column Check

MediumSQL · PostgreSQL

Problem

Write SQL queries to check if the row is a primary column.

Use the supplied metadata tables to determine whether each registered column belongs to a primary key constraint. Include columns without matching constraints and distinguish primary key constraints from other constraint types.

Output

  1. One row per registered column.
  2. Columns: table_schema, table_name, column_name, and is_primary_key.
  3. Sort by schema, table name, and column ID.

Schema

catalog_columns
ColumnTypeDescription
column_idPKINTUnique metadata identifier for a column
table_idINTIdentifier of the table containing the column
table_schemaVARCHAR(63)Database schema name
table_nameVARCHAR(63)Table name
column_nameVARCHAR(63)Column name
table_constraints
ColumnTypeDescription
constraint_idPKINTUnique constraint identifier
table_idINTIdentifier of the constrained table
constraint_nameVARCHAR(100)Constraint name
is_primary_keyBOOLEANWhether the constraint is a primary key
constraint_columns
ColumnTypeDescription
constraint_idPKINTReferenced constraint identifier
column_idPKINTReferenced column identifier
ordinal_positionINTColumn position within the constraint
Tablescatalog_columnstable_constraintsconstraint_columns
Interviewer

Your question is Primary Key Column Check. Start with the requirements and the three tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.