October 21, 2024 By Matthew Rathbone

SQL-Based Tools

1. Beekeeper Studio

Beekeeper Studio is an open-source SQL editor and database manager that provides a simple yet powerful interface for querying SQL databases. It supports multiple databases like MySQL, PostgreSQL, SQLite, and others.

Example Query: Retrieving Employee Data

Here’s how you can use Beekeeper Studio to query a PostgreSQL database for employee data:

SELECT employee_id, first_name, last_name, department
FROM employees
WHERE department = 'Engineering'
ORDER BY last_name;

Expected Output:

employee_id first_name last_name department
103 Jane Doe Engineering
105 John Smith Engineering
110 Alice Johnson Engineering

This SQL query selects employee details from the employees table where the department is ‘Engineering’ and orders the results by the last_name.

2. DBeaver

DBeaver is a universal database management tool that supports a wide range of databases, including SQL and NoSQL. It offers a rich SQL editor with auto-completion, syntax highlighting, and execution history.

Example Query: Aggregating Sales Data

Using DBeaver to connect to a MySQL database, you can execute aggregate functions like this:

SELECT product_id, SUM(quantity) AS total_quantity, SUM(price * quantity) AS total_sales
FROM sales
GROUP BY product_id
ORDER BY total_sales DESC;

Expected Output:

product_id total_quantity total_sales
P1001 500 7500.00
P1005 320 6400.00
P1003 250 5000.00

This query aggregates sales data by product_id, calculating the total quantity sold and the total sales amount. The results are ordered by total_sales in descending order.

3. SQL Workbench/J

SQL Workbench/J is a free, cross-platform SQL query tool compatible with most SQL-based databases. It offers extensive SQL script editing features, result set handling, and export options.

Example Query: Updating Product Information

An example of using SQL Workbench/J to update a table in a PostgreSQL database:

UPDATE products
SET price = price * 1.10
WHERE category = 'Electronics';

Expected Output:

Query OK, 5 rows affected

This query modifies the products table, applying a 10% price increase to items in the ‘Electronics’ category using the UPDATE statement.

NoSQL Querying Tools

1. MongoDB Compass

MongoDB Compass is a GUI for MongoDB that allows you to explore and interact with your data without writing extensive code. It provides a visual environment for MongoDB queries.

Example Query: Filtering Documents

Using MongoDB Compass to find documents in a customers collection:

{
  "age": { "$gt": 30 },
  "subscription_status": "active"
}

Expected Output

[
  {
    "_id": "60b8d75d8e3f4a6c4e000001",
    "name": "Alice Johnson",
    "age": 35,
    "subscription_status": "active"
  },
  {
    "_id": "60b8d75d8e3f4a6c4e000002",
    "name": "Bob Smith",
    "age": 45,
    "subscription_status": "active"
  }
]

This query filters documents in the customers collection where age is greater than 30 and subscription_status is ‘active’.

2. Couchbase Query Workbench

Couchbase Query Workbench provides an interface for executing N1QL (SQL-like) queries on Couchbase data. It’s designed to handle JSON documents in a SQL-like fashion.

Example Query: Selecting Nested Fields

SELECT name, address.city
FROM customers
WHERE address.state = 'CA';

Expected Output:

name city
Alice Johnson San Diego
Bob Smith Los Angeles

This N1QL query selects name and the nested city field from the address object in the customers documents where the state is ‘CA’.

APIs and Query Builders

1. GraphQL

GraphQL is an API query language for requesting data in a flexible manner. It allows clients to request specific fields from the server.

Example Query: Fetching Nested Data

{
  employee(id: "103") {
    firstName
    lastName
    department {
      name
      location
    }
  }
}

Expected Output:

{
  "data": {
    "employee": {
      "firstName": "Jane",
      "lastName": "Doe",
      "department": {
        "name": "Engineering",
        "location": "New York"
      }
    }
  }
}

This GraphQL query requests specific fields (firstName, lastName, department.name, and department.location) for the employee with id: 103.

2. ORMs (Object-Relational Mappers)

ORMs like Active Record (for Ruby on Rails), Sequelize (for Node.js) and Django ORM (for Python) offer a way to query databases using code instead of raw SQL. This is useful when integrating database queries directly into an application.

Example Query: Using Sequelize in Node.js

const employees = await Employee.findAll({
  where: {
    department: 'Engineering'
  },
  order: [['lastName', 'ASC']]
});

Expected Output:

An array of JavaScript objects representing employees in the ‘Engineering’ department:

[
  { "employee_id": 103, "first_name": "Jane", "last_name": "Doe", "department": "Engineering" },
  { "employee_id": 105, "first_name": "John", "last_name": "Smith", "department": "Engineering" }
]

The ORM automatically converts the JavaScript code into an SQL query that fetches and orders employee data from the database.

Conclusion

Different data querying tools serve various purposes depending on the type of database (SQL vs. NoSQL) and the user’s specific needs. SQL-based tools like Beekeeper Studio, DBeaver, and SQL Workbench/J excel in handling structured data. NoSQL tools like MongoDB Compass and Couchbase Query Workbench allow flexibility in querying unstructured data. APIs like GraphQL and ORMs offer programmatic ways to interact with databases in modern applications.

Beekeeper Studio Is A Free & Open Source Database GUI

Best SQL query & editor tool I have ever used. It provides everything I need to manage my database. - ⭐⭐⭐⭐⭐ Mit

Beekeeper Studio is fast, intuitive, and easy to use. Beekeeper supports loads of databases, and works great on Windows, Mac and Linux.

Beekeeper's Linux version is 100% full-featured, no cut corners, no feature compromises.