{"id":7340,"date":"2022-05-18T05:28:04","date_gmt":"2022-05-18T12:28:04","guid":{"rendered":"https:\/\/coderpad.io\/?post_type=doc&#038;p=7340"},"modified":"2025-07-25T03:58:13","modified_gmt":"2025-07-25T10:58:13","slug":"databases","status":"publish","type":"doc","link":"https:\/\/coderpad.io\/resources\/docs\/interview\/databases\/","title":{"rendered":"Databases"},"content":{"rendered":"\n<p>The Interview platform allows you to create custom databases and attach them to questions to assess a candidate&#8217;s data processing skills. Click on an item in the table of contents below to learn more about using the database feature.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><nav aria-label=\"Table of Contents\"><ul><li class=\"\"><a href=\"#creating-a-custom-database\">Creating a custom database<\/a><ul><li class=\"\"><a href=\"#questions\">Questions<\/a><\/li><li class=\"\"><a href=\"#schema\">Schema<\/a><\/li><li class=\"\"><a href=\"#edit\">Edit<\/a><\/li><\/ul><\/li><li class=\"\"><a href=\"#add-a-database-to-a-question\">Add a database to a question<\/a><\/li><li class=\"\"><a href=\"#accessing-the-database-in-the-interview\">Accessing the database in the interview<\/a><ul><li class=\"\"><a href=\"#multi-file-languages\">Via an ORM or Adaptor<\/a><\/li><li class=\"\"><a href=\"#single-file-languages\">Via SQL<\/a><ul><li class=\"\"><a href=\"#via-an-orm-or-adaptor\">Via an ORM or Adaptor<\/a><\/li><li class=\"\"><a href=\"#via-sql\">Via SQL<\/a><\/li><\/ul><\/li><\/ul><\/li><li class=\"\"><a href=\"#important-reminders\">Important reminders<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-a-custom-database\">Creating a custom database<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2705  If you&#8217;re using a multi-file language <a href=\"https:\/\/coderpad.io\/resources\/docs\/interview\/question-bank\/creating-a-question\/\">you can add a database file directly to the file directory when you create the question<\/a>.<\/p>\n\n\n\n<p>For example, a Node pad runs a Node.js Express application with a built-in connection to a PostgreSQL database. We&#8217;ve also installed Sequelize, a promise-based ORM tool that simplifies interactions with the database. You can find more details in the <code>server.ts<\/code> file. To set up a database in a Node pad, you can either run a database initialization script or do so via an API endpoint, like those in <code>server.ts<\/code>.<\/p>\n<\/blockquote>\n\n\n\n<p>You can upload custom databases and attach them to questions to test a candidate\u2019s ability to manage, write, and edit database queries either using SQL or a <a href=\"#adaptors\">language ORM\/adaptor<\/a>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2705 Interview&#8217;s custom databases are available for MySQL and PostgreSQL.<\/p>\n<\/blockquote>\n\n\n\n<p>To start, navigate to your Interview dashboard and click <strong>Databases <\/strong>in the left-side navigation bar to go to the <em>Custom Databases<\/em> page.&nbsp;Then click <strong>Create Database<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-9.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"396\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-9-1024x396.png\" alt=\"Custom database page. The &quot;Databases&quot; item is selected in the left nav, and the &quot;create database&quot; button is highlighted at the top right of the scren.\" class=\"wp-image-34724\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-9-1024x396.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-9-300x116.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-9-768x297.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-9-1536x594.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-9-18x7.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-9.png 1809w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>Add a <em>Title<\/em>, <em>Description<\/em>, and select <code>MySQL<\/code> or <code>PostgreSQL<\/code> from the <em>Language <\/em>dropdown.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/03\/image-58.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"494\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/03\/image-58-1024x494.png\" alt=\"The new custom database window. There are fields for title, language, and description.\" class=\"wp-image-32376\" style=\"width:840px;height:405px\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/03\/image-58-1024x494.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/03\/image-58-300x145.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/03\/image-58-768x371.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/03\/image-58-1536x741.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/03\/image-58-18x9.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/03\/image-58.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>Below that you&#8217;ll see the SQL script that will create the sample database. You can edit this as needed to create your desired database; alternatively you can copy and paste SQL from an existing database into the text box for further customization. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><a href=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/01\/img_61e8690aef94e.png\"><img decoding=\"async\" src=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/01\/img_61e8690aef94e.png\" alt=\"At the bottom of the creation screen is a field to enter a DB creation script.\" style=\"width:800px;height:340px\"\/><\/a><figcaption class=\"wp-element-caption\">Interview provides a sample database creation script which includes employees, projects, and department tables. <\/figcaption><\/figure>\n<\/div>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2705 The easiest way to get the raw SQL from an existing database in order to upload it to Interview is with <code>mysqldump<\/code> for MySQL, or <code>pg_dump<\/code> for PostgreSQL. You can upload up to 5 MB of raw SQL.<\/p>\n<\/blockquote>\n\n\n\n<p>Verify the details and click <strong>Save <\/strong>to create the database.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-10.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"622\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-10-1024x622.png\" alt=\"the new custom database screen with the save button highlighted at the bottom left of the screen.\" class=\"wp-image-34726\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-10-1024x622.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-10-300x182.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-10-768x467.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-10-18x12.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-10.png 1466w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>You\u2019ll see a notification that your database is initializing.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2757If you see an error message, it means your SQL is structured incorrectly. Please correct the errors and try again.&nbsp;<\/p>\n<\/blockquote>\n\n\n\n<p>Once the database is initialized, you\u2019ll see the <em>Overview<strong> <\/strong><\/em>tab which will give you a summary of your custom database. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-11.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"434\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-11-1024x434.png\" alt=\"The database verification screen. There is a database summary as well as a table schema to view.\" class=\"wp-image-34728\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-11-1024x434.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-11-300x127.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-11-768x326.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-11-18x8.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-11.png 1061w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>You&#8217;ll also see the following tabs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"questions\"><strong>Questions<\/strong><\/h3>\n\n\n\n<p>The <em>Questions<\/em> tab will show you any questions that have the database attached to them. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-14.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"428\" height=\"255\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-14.png\" alt=\"The questions tab is shown with the text &quot;this database is attached to one question&quot;, and the question is linked below.\" class=\"wp-image-34735\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-14.png 428w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-14-300x179.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-14-18x12.png 18w\" sizes=\"auto, (max-width: 428px) 100vw, 428px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>Clicking on the question will open it up in the question bank dashboard.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"schema\"><strong>Schema<\/strong><\/h3>\n\n\n\n<p>The <em>Schema <\/em>tab will display the database scheme. You can switch the toggle at the top to switch between the <em>Explorer <\/em>view and the <em>Raw <\/em>view.<\/p>\n\n\n\n<p>The <em>Raw <\/em>view displays the standard SQL schema definition:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-15.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"471\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-15.png\" alt=\"The following is displayed in raw sql: &quot;DESCRIBE customers;\n+---------+--------------+------+-----+---------+----------------+\n| Field | Type | Null | Key | Default | Extra |\n+---------+--------------+------+-----+---------+----------------+\n| id | int | NO | PRI | NULL | auto_increment |\n| name | varchar(255) | NO | | NULL | |\n| email | varchar(255) | NO | | NULL | |\n| phone | varchar(20) | NO | | NULL | |\n| address | varchar(255) | NO | | NULL | |\n+---------+--------------+------+-----+---------+----------------+\nFOREIGN KEYS;&quot;\" class=\"wp-image-34737\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-15.png 1000w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-15-300x141.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-15-768x362.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-15-18x8.png 18w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>The <em>Explorer<\/em> view displays a graphical representation of the database with tables and relationships between the tables. You can zoom in, zoom out, and click on the screen to move around the schema.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-16.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"762\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-16-1024x762.png\" alt=\"A graphical represntation of the customers table is shown with id, name, ameail, phone, and address fields shown along with their respective data types.\" class=\"wp-image-34740\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-16-1024x762.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-16-300x223.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-16-768x572.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-16-16x12.png 16w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-16.png 1033w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2705 Both the interviewer and candidate will be able to utilize the <em>Raw <\/em>and <em>Explorer <\/em>views in the pad.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"edit\"><strong>Edit<\/strong><\/h3>\n\n\n\n<p>The <em>Edit <\/em>tab will open the customization page so you can make any changes to the database that need to be made.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-17.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"795\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-17-1024x795.png\" alt=\"The database customization window is open with the title, language, description, and script fields shown. \" class=\"wp-image-34744\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-17-1024x795.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-17-300x233.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-17-768x596.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-17-15x12.png 15w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-17.png 1036w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2705 You can reach the <em>Overview<\/em>, <em>Questions, Schema, <\/em>and <em>Edit<\/em> tab options at anytime by navigating to the <em>Custom Database<\/em> page from the left nav menu and then clicking on the <strong>View<\/strong> button of the database you want to access.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-12.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"53\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-12-1024x53.png\" alt=\"A row on the custom database page with an arrow pointing to the &quot;view&quot; button.\" class=\"wp-image-34730\" style=\"width:840px;height:43px\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-12-1024x53.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-12-300x15.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-12-768x39.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-12-18x1.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-12.png 1460w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div><\/blockquote>\n\n\n\n<p>In the <strong>Databases <\/strong>tab on the dashboard you\u2019ll now see your new database in the list of available custom databases.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-13.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"323\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-13-1024x323.png\" alt=\"The new database is shown in the databases list in the dashboard.\" class=\"wp-image-34733\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-13-1024x323.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-13-300x95.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-13-768x242.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-13-18x6.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-13.png 1429w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u26a0\ufe0f To delete a database, simple click on the three dots to the right of the row and select <strong>Delete<\/strong>. Keep in mind that this operation is permanent. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-18.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"80\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-18-1024x80.png\" alt=\"A database row is shown with the three vertical dots and delete option highlighted.\" class=\"wp-image-34747\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-18-1024x80.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-18-300x23.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-18-768x60.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-18-18x1.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-18.png 1400w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"add-a-database-to-a-question\"><strong>Add a database to a question<\/strong><\/h2>\n\n\n\n<p>Custom databases must be associated with a question from the Question Bank. You can do this in <em>Step 2 &#8211; Add Code Elements<\/em> of question creation or editing by simply selecting your database from the <em>Custom Database <\/em>dropdown menu.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/image-63.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"388\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/image-63-1024x388.png\" alt=\"The &quot;add code elements&quot; page with the &quot;Custom databases&quot; dropdown menu highlighted. \" class=\"wp-image-24275\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-63-1024x388.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-63-300x114.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-63-768x291.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-63-1536x582.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-63-18x7.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-63.png 1734w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>When you load the question into a pad, the database will be available for querying either directly through MySQL\/PostgresSQL, or through an ORM\/adaptor for languages that have them.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-20.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"497\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-20-1024x497.png\" alt=\"A pad with the databases tab and printout shown in the right window.\" class=\"wp-image-34767\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-20-1024x497.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-20-300x146.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-20-768x373.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-20-1536x746.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-20-18x9.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-20.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"accessing-the-database-in-the-interview\"><strong>Accessing the database in the interview<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"multi-file-languages\"><strong>Multi-file languages<\/strong><\/h3>\n\n\n\n<p>You&#8217;ll need to use a connector like in one of these Python examples.<\/p>\n\n\n\n<p><strong>Example 1: <\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\">engine = sqlalchemy.create_engine(<span class=\"hljs-string\">'postgresql:\/\/coderpad@\/coderpad?host=\/tmp\/postgresql\/socket'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">read_root<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-comment\"># Replace these with your actual MySQL server information<\/span>\n    socket_path = <span class=\"hljs-string\">'\/tmp\/mysql\/socket\/mysqld.sock'<\/span>\n    user = <span class=\"hljs-string\">\"coderpad\"<\/span>\n    database_name = <span class=\"hljs-string\">\"coderpad\"<\/span>\n\n    <span class=\"hljs-keyword\">try<\/span>:\n        <span class=\"hljs-comment\"># Create a connection to the MySQL server using the socket<\/span>\n        connection = mysql.connector.connect(\n            user=user,\n            unix_socket=socket_path,\n            database=database_name\n        )\n\n        <span class=\"hljs-keyword\">if<\/span> connection.is_connected():\n            print(<span class=\"hljs-string\">\"Connected to MySQL server\"<\/span>)\n\n        <span class=\"hljs-comment\"># Your code for executing SQL queries or operations goes here<\/span>\n        cursor = connection.cursor()\n        <span class=\"hljs-comment\"># Execute your SQL queries here<\/span>\n        cursor.execute(<span class=\"hljs-string\">\"SELECT * FROM employees\"<\/span>)\n\n        <span class=\"hljs-comment\"># Fetch and consume the results<\/span>\n        results = cursor.fetchall()\n        print(results)\n\n    <span class=\"hljs-keyword\">except<\/span> mysql.connector.Error <span class=\"hljs-keyword\">as<\/span> error:\n        print(<span class=\"hljs-string\">f\"Error: <span class=\"hljs-subst\">{error}<\/span>\"<\/span>)\n    <span class=\"hljs-keyword\">finally<\/span>:\n        <span class=\"hljs-comment\"># Close the connection when done<\/span>\n        <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-string\">'connection'<\/span> <span class=\"hljs-keyword\">in<\/span> locals() <span class=\"hljs-keyword\">and<\/span> connection.is_connected():\n            connection.close()\n            print(<span class=\"hljs-string\">\"Connection closed\"<\/span>)\n    <span class=\"hljs-keyword\">return<\/span> {<span class=\"hljs-string\">\"Hello\"<\/span>: <span class=\"hljs-string\">\"World\"<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"single-file-languages\"><strong>Single-file languages<\/strong><\/h3>\n\n\n\n<p>Once you&#8217;ve created your question and custom database, the database will automatically be accessible when you create the pad. <\/p>\n\n\n\n<p>Both the interviewer and candidate will have access to the database schema in the&nbsp;<em>Database<\/em>&nbsp;tab at the top of the right pad pane (1). You can switch between the&nbsp;<em>Raw&nbsp;<\/em>and&nbsp;<em>Explorer<\/em>&nbsp;mode with the toggle button (2). <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-23.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"498\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-23-1024x498.png\" alt=\"The database tab is open with the explorer option selected and a customers table displayed. There is a 1 next to the &quot;database&quot; tab at the top, and a 2 next to the toggle button above the schema.\" class=\"wp-image-34773\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-23-1024x498.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-23-300x146.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-23-768x373.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-23-1536x746.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-23-18x9.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-23.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>Like in the database creation step above, in&nbsp;<em>Explorer&nbsp;<\/em>mode, you can zoom out of the diagram and click and hold the right mouse button to move around the screen:<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"928\" style=\"aspect-ratio: 1912 \/ 928;\" width=\"1912\" autoplay controls loop muted src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/dbvideo.mp4\"><\/video><\/figure>\n\n\n\n<p>As far as accessing the data in the database programmatically, there are two ways to do that within the pad:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#adaptors\">Via an ORM or Adaptor<\/a><\/li>\n\n\n\n<li><a href=\"#sql\">Via SQL<\/a><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"via-an-orm-or-adaptor\"><strong>Via an ORM or Adaptor:<\/strong><\/h4>\n\n\n\n<p>Interview provides common ORM\/adaptor libraries so you can directly query databases from non-database language environments. For example, you can now have candidates load SQL records and interact with them in Python, or show their <code>ActiveRecord<\/code> skills within the Ruby environment.<\/p>\n\n\n\n<p>This feature is available for the most commonly used languages in Interview, preconfigured with the adaptors listed below. Additional languages may be added based on customer needs.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Java &#8211;&nbsp;<a class=\"rank-math-link\" href=\"https:\/\/jdbc.postgresql.org\/\" target=\"_blank\" rel=\"noopener\">JDBC adaptor<\/a><\/li>\n\n\n\n<li>Javascript &#8211;&nbsp;<a href=\"https:\/\/sequelize.org\/\" target=\"_blank\" rel=\"noopener\">Sequelize<\/a>,&nbsp;<a href=\"https:\/\/typeorm.io\/#\/\" target=\"_blank\" rel=\"noopener\">TypeORM<\/a><\/li>\n\n\n\n<li>Python 2 &amp; 3 &#8211;&nbsp;<a href=\"https:\/\/www.sqlalchemy.org\/\" target=\"_blank\" rel=\"noopener\">SQLAlchemy<\/a><\/li>\n\n\n\n<li>C++ &#8211;&nbsp;<a href=\"https:\/\/dev.mysql.com\/doc\/connector-j\/8.0\/en\/connector-j-overview.html\" target=\"_blank\" rel=\"noopener\">MySQL Connector<\/a>,&nbsp;<a href=\"http:\/\/pqxx.org\/development\/libpqxx\/#technical-overview\" target=\"_blank\" rel=\"noopener\">libpqxx (PostgreSQL)<\/a><\/li>\n\n\n\n<li>Ruby &#8211;&nbsp;<a href=\"https:\/\/guides.rubyonrails.org\/active_record_querying.html\" target=\"_blank\" rel=\"noopener\">ActiveRecord<\/a>,&nbsp;<a href=\"https:\/\/sequel.jeremyevans.net\/documentation.html\" target=\"_blank\" rel=\"noopener\">Sequel<\/a><\/li>\n\n\n\n<li>C# &#8211;&nbsp;<a href=\"https:\/\/dapper-tutorial.net\/dapper\" target=\"_blank\" rel=\"noopener\">Dapper<\/a>,&nbsp;<a class=\"rank-math-link\" href=\"https:\/\/docs.microsoft.com\/en-us\/ef\/\" target=\"_blank\" rel=\"noopener\">Entity Framework<\/a>,&nbsp;<a class=\"rank-math-link\" href=\"https:\/\/dev.mysql.com\/doc\/connector-net\/en\/connector-net-introduction.html\" target=\"_blank\" rel=\"noopener\">MySQL Connector<\/a>,&nbsp;<a href=\"https:\/\/www.npgsql.org\/doc\/basic-usage.html\" target=\"_blank\" rel=\"noopener\">Npgsql<\/a><\/li>\n<\/ul>\n\n\n\n<p>To setup data access through an adaptor, you&#8217;ll first need to select one of the supported languages listed above from the language dropdown. <\/p>\n\n\n\n<p>Once you do this, you\u2019ll see a new <strong>Database adapters <\/strong>menu appear next to the language; this menu auto-loads the database adaptor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/03\/image-59.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"213\" height=\"263\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/03\/image-59.png\" alt=\"The database adapters menu is shown and lists 4 different ORMs for javascript.\" class=\"wp-image-32378\" style=\"height:303px\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/03\/image-59.png 213w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/03\/image-59-10x12.png 10w\" sizes=\"auto, (max-width: 213px) 100vw, 213px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>Click on the dropdown, and choose whether you\u2019d like to use the <strong>MySQL <\/strong>or <strong>PostgreSQL <\/strong>adaptor. Selecting an adaptor automatically loads example adaptor\/ORM code in the left hand pane (1). You can see your database schema in the right hand pane under the <strong>Database<\/strong> tab (2):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-21.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"497\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/06\/image-21-1024x497.png\" alt=\"On the left side the stub code for the database adaptor is shown, and on the right the database schema is shown under the database tab.\" class=\"wp-image-34769\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-21-1024x497.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-21-300x146.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-21-768x373.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-21-1536x746.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-21-18x9.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/06\/image-21.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2757Loading the DB adaptor code will comment out all existing code, including any question code. Best practice is to load the question at the start of the interview, and you may need to rearrange code a little once you load the adaptor. This approach does provide the most flexibility in candidate interview language choice or preferred DB adaptor.<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2705 If you do not have a database attached to a question when you select an adaptor package, the pad will load a sample database for you.<\/p>\n<\/blockquote>\n\n\n\n<p>Keep in mind that the adaptor package is just helper code. <a href=\"#add-a-database-to-a-question\">As mentioned above<\/a>, you also have the option to paste in the adaptor code when setting up a question. <strong>This lets you skip the package loading step, as the database will always be loaded with the question, not the package. <\/strong><\/p>\n\n\n\n<p>This does require you to select a particular language and adaptor for the candidate to use when setting up the question, so there is less flexibility at interview time.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"via-sql\"><strong>Via SQL:<\/strong><\/h4>\n\n\n\n<p>Candidates can write and run SQL queries against the database at interview in the code editor. Once you select your SQL flavor (MySQL or PostgreSQL), there is no further configuration required.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/image-68.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"468\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/image-68-1024x468.png\" alt=\"SQL input is on the left and the results of the SQL query is on the right.\" class=\"wp-image-24319\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-68-1024x468.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-68-300x137.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-68-768x351.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-68-1536x702.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-68-18x8.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/11\/image-68.png 1914w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"important-reminders\">Important reminders<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When you set up your own custom database, the original DB will always be saved and never edited; a copy is loaded for each new interview session.<\/li>\n\n\n\n<li>Edits \u2013 such as row inserts, updates and deletes \u2013 are stateful during a specific interview session and persist between subsequent code execution.<\/li>\n\n\n\n<li>You can reset the DB back to its original unmodified state during a specific interview by pressing the <strong>Reset <\/strong>button in the top right corner.<\/li>\n<\/ul>\n","protected":false},"parent":16734,"menu_order":6,"template":"","class_list":["post-7340","doc","type-doc","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/doc\/7340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/doc"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/doc"}],"version-history":[{"count":72,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/doc\/7340\/revisions"}],"predecessor-version":[{"id":42938,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/doc\/7340\/revisions\/42938"}],"up":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/doc\/16734"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=7340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}