SQL Basics
About the Lab
In this exercise, we will explore the basics of SQL queries and learn how to interact with databases using commonly used SQL statements.
The lab covers:
SELECTCREATEINSERTUPDATEUNIONJOIN
These queries demonstrate how to retrieve, create, modify, combine, and relate data within a database.
Login to the Application
Use the provided credentials to access the application:
Username: admin
Password: adminEnter the credentials in the login form and proceed to the application.
Step 1: Inspect the Database
Expand the Databases section in the left panel and select any table under the wpdatabase database.
The selected table’s details and structure will be displayed in the Properties tab, allowing you to inspect its database information.
Step 2: View the Table Definition
Click on the DDL tab to view the Data Definition Language statement for the selected table.
The DDL tab displays the table’s definition, including its columns, data types, and other structural information.
Step 3: Inspect the wp_commentmeta Table
Expand the wp_commentmeta table and its child tree from the left panel.
The table structure can be explored to view details such as:
- Columns
- Primary keys
- Foreign keys
- Indexes
This provides an overview of how the table is structured and how its data is organized.
Step 4: Inspect Column Details
Expand the Columns section of the wp_commentmeta table.
The available columns and their data types and properties are displayed, providing details about how each field is defined in the table.
Getting Started with Basic SQL Queries
Task 1: Identify the Databases
To list all databases available on the system, use the SHOW DATABASES statement.
Query:
SHOW DATABASES;This displays the databases currently available on the SQL server.
Task 2: Select a Database
Use the USE statement to select the wpdatabase database for subsequent queries.
Query:
USE wpdatabase;This sets wpdatabase as the current database.
Task 3: List Tables in the Database
Use the SHOW TABLES statement to display all tables available in the currently selected database.
Query:
SHOW TABLES;This lists all tables present in the wpdatabase database.
Task 4: Retrieve Data from a Table
Use the SELECT statement to retrieve all records and columns from the wp_users table.
Query:
SELECT * FROM wp_users;This displays all available data stored in the wp_users table.
Task 5: Retrieve Specific Columns
Instead of retrieving all columns, you can specify the columns you want to display using the SELECT statement.
Query:
SELECT user_login, user_pass FROM wp_users;This query retrieves only the user_login and user_pass columns from the wp_users table.
Task 6: Retrieve Data by Specifying the Database
You can directly reference a table from a specific database using the database.table format.
Query:
SELECT * FROM wpdatabase.wp_posts;This retrieves all columns and records from the wp_posts table in the wpdatabase database.
Task 7: Sort Data by Post Title
Use the ORDER BY clause to sort the retrieved records based on the post_title column.
Query:
SELECT * FROM wpdatabase.wp_posts ORDER BY post_title;This displays the records from wp_posts sorted by post_title in ascending order by default.
Task 8: Retrieve the First Record
Use the LIMIT clause to restrict the query result to a single record after sorting the data by post_title.
Query:
SELECT * FROM wpdatabase.wp_posts ORDER BY post_title LIMIT 1;This returns the first record based on the ascending order of post_title.
Task 9: Retrieve a Particular Row
Use LIMIT with an offset to retrieve a specific row from the sorted results.
Query:
SELECT * FROM wpdatabase.wp_posts ORDER BY post_title LIMIT 2,1;Here, 2 is the offset, which skips the first two rows, and 1 specifies that only one row should be returned. This retrieves the third row after sorting by post_title.
Task 10: Create a New Table
Create a new table named new_users with name, email, and id columns. The id column is defined as the primary key.
Query:
CREATE TABLE new_users (
name VARCHAR(100),
email VARCHAR(100),
id VARCHAR(100),
PRIMARY KEY (id)
);This creates the new_users table with the specified columns and ensures that each id value is unique.
Task 11: Insert a Row into the Table
Use the INSERT INTO statement to add a new record to the new_users table.
Query:
INSERT INTO wpdatabase.new_users
VALUES ("Nidhin", "Nidhin@Medium.com", "pa-05678");This inserts a new row containing the name, email, and ID values into the new_users table.
Task 12: Update a Record in the Table
Use the UPDATE statement to modify an existing record in the new_users table.
Query:
UPDATE wpdatabase.new_users
SET name = "itx_nidhin"
WHERE id = "pa-05678";This updates the name value of the record whose id is pa-05678.
Task 13: Combine Records Using the UNION Statement
The UNION operator combines the results of two SELECT queries into a single result set. Both queries must return the same number of columns with compatible data types.
First, inspect the columns of the wp_users table:
DESC wpdatabase.wp_users;Then inspect the columns of the new_users table:
DESC wpdatabase.new_users;After identifying compatible columns, use:
SELECT user_login, user_email
FROM wpdatabase.wp_users
UNION
SELECT name, email
FROM wpdatabase.new_users;The results from both wp_users and new_users are combined and displayed as a single result set.
Task 14: Retrieve Common Users Using JOIN
Insert additional users into the new_users table:
INSERT INTO wpdatabase.new_users
VALUES ("admin", "admin@admin.xyz", "pa-02333");INSERT INTO wpdatabase.new_users
VALUES ("jimmy", "jimmy@attackdefense.com", "pa-02323");INSERT INTO wpdatabase.new_users
VALUES ("david", "david@attackdefense.com", "pa-02663");
View the records in the table:
SELECT * FROM wpdatabase.new_users;To retrieve users with matching names in both wp_users and new_users, use a JOIN:
SELECT *
FROM wpdatabase.wp_users
JOIN wpdatabase.new_users
ON wpdatabase.new_users.name = wpdatabase.wp_users.user_login;The query matches user_login from wp_users with name from new_users and returns the corresponding columns from both tables.
Task 15: Retrieve Specific Fields Using JOIN
Use the JOIN statement to retrieve selected columns from both tables. The query displays user_login and user_pass from wp_users, along with the id from new_users.
Get Nidhindev’s stories in your inbox
Join Medium for free to get updates from this writer.
Query:
SELECT wpdatabase.wp_users.user_login,
wpdatabase.wp_users.user_pass,
wpdatabase.new_users.id
FROM wpdatabase.wp_users
JOIN wpdatabase.new_users
ON wpdatabase.new_users.name = wpdatabase.wp_users.user_login;The query returns the matching users along with their corresponding login, password, and ID values.
Task 16: Use Aliases for Columns
Use the AS keyword to assign aliases to the user_login and user_pass columns.
Query:
SELECT user_login AS name,
user_pass AS password
FROM wpdatabase.wp_users;This displays user_login as name and user_pass as password in the query results, without changing the original column names in the database.
Task 17: Identify Distinct Authors
Use the DISTINCT keyword to retrieve unique author IDs from the post_author column in the wp_posts table.
Query:
SELECT DISTINCT(post_author)
FROM wpdatabase.wp_posts;The query returns only the unique author IDs associated with published posts. In this lab, only one author has published a post.
Task 18: Count Posts
Use the COUNT() aggregate function to determine the total number of records stored in the wp_posts table.
Query:
SELECT COUNT(*)
FROM wpdatabase.wp_posts;This returns the total number of rows present in the wp_posts table.
Task 19: Retrieve Published Posts
Use the WHERE clause to filter the records based on the post_status column.
Query:
SELECT *
FROM wpdatabase.wp_posts
WHERE post_status = 'publish';This query displays all posts whose status is set to publish.
Task 20: Search Posts by Content
Use the LIKE operator with wildcard characters to find posts containing the string wp:paragraph in the post_content column.
Query:
SELECT *
FROM wpdatabase.wp_posts
WHERE post_content LIKE '%wp:paragraph%';This returns all posts whose content contains wp:paragraph anywhere within the post_content field.
Task 21: Filter Posts Using Multiple Conditions
Use the WHERE clause with the AND operator to retrieve posts that meet both conditions:
post_statusis set topublishcomment_statusis set toclosed
Query:
SELECT *
FROM wpdatabase.wp_posts
WHERE post_status = "publish"
AND comment_status = "closed";This returns only the posts that satisfy both conditions.
The table data can also be viewed in OmniDB by right-clicking the table and selecting Query data. OmniDB also provides options to edit tables, insert records, and update records through its interface.
Getting Started with phpMyAdmin
The next section of the lab focuses on using phpMyAdmin to interact with the database through a web-based interface.
The phpMyAdmin login page is provided as the starting point for accessing and managing the database.
Use the credentials provided in the challenge description to log in to phpMyAdmin:
Username: root
Password: <No Password>After successful authentication, the phpMyAdmin main dashboard is displayed, providing access to the available databases and database management features.
The available databases are displayed in the left panel of phpMyAdmin.
Expand the wpdatabase database to view its tables and explore the available database structure.
All tables within the wpdatabase database are displayed after expanding the database.
Expand the wp_users table to view its structure. The left panel displays information about the table's columns and indexes.
View Available Databases
Click on the Databases tab in phpMyAdmin.
The page displays a list of all databases available on the server, providing an overview of the database environment.
View Tables in wpdatabase
Click on the wpdatabase database from the database list.
The tables contained within the wpdatabase database are displayed, allowing you to explore and manage the available tables.
View Records in wp_users
Click on the wp_users table.
The records stored in the table are displayed on the web page, allowing you to view the available user data.
View Table Structure
Click on the Structure tab to view the structural information of the selected table.
This displays details such as the columns, data types, indexes, and keys defined for the table.
Execute SQL Queries Using the SQL Tab
Click on the SQL tab in phpMyAdmin. This section allows you to execute SQL queries directly against the selected database.
The same SQL queries used earlier in the OmniDB SQL console can also be executed here.
phpMyAdmin provides options such as Select, Insert, and Update to help modify and construct SQL statements.
Search Table Data
Click on the Search tab to search for records without manually writing a SELECT statement with a WHERE clause.
The search interface allows you to enter values and choose different comparison operators to filter the table data directly.
Insert Records
Click on the Insert tab to add new records to the selected table.
The interface provides fields for entering the required values, which can then be submitted to insert the new record into the database.
Export and Import Data
Click on the Export tab to export the selected table and its data in a supported format.
The Import section can be used to import an SQL archive file and restore its contents into the database.
View Database Privileges
Click on the Privileges tab to view the permissions assigned to users for the wpdatabase database.
The tab displays the available privileges and access levels associated with the database.
Operations Tab
Click on the Operations tab to access various table management options.
The Operations tab allows you to perform actions such as:
- Moving a table
- Creating a copy of the table
- Renaming the table
- Adding table comments
These options provide additional ways to manage the selected table.
View Triggers
Click on the Triggers tab to view the triggers associated with the selected table.
Currently, there are no triggers configured for the table.
Return to Dashboard
Click on the Dashboard or Home icon to return to the phpMyAdmin main dashboard.
The dashboard provides access to the available databases and overall database management options.
Execute SQL Statements
Click on the SQL tab from the dashboard.
SQL statements can be executed directly from this section. The queries previously executed in the OmniDB SQL console can also be run here.
View User Accounts
Click on the User accounts tab to view the user accounts configured in phpMyAdmin.
The page displays the available database users along with their associated access and privilege information.
View the Binary Log
Click on the Binary Log tab to view the MySQL binary log information.
The binary log records database changes and SQL statements related to logged database operations, which can be useful for auditing and replication.
Conclusion
This lab provided a practical introduction to SQL and database management using OmniDB and phpMyAdmin. We worked with essential SQL statements such as SELECT, CREATE, INSERT, UPDATE, UNION, and JOIN to retrieve and manipulate database records.
We also explored database and table structures, column properties, indexes, privileges, triggers, and various phpMyAdmin features for managing database content. Overall, the lab helped build a strong foundation in SQL queries and practical database administration.







