Skip to main content

SQL Query

New SQL Query Creation

The New SQL Query interface allows users to create, edit, and manage SQL queries for their databases. The tool offers support for the four primary SQL operations (SELECT, INSERT, UPDATE, and DELETE) while also incorporating features like AI-assisted query generation, parameter management, and context selection to ensure queries are accurate and efficient.

AI-Generated Queries Using AI

The Generate Queries Using AI section allows you to leverage artificial intelligence to automatically generate SQL queries based on simple instructions and the context of the table you are working with. This feature streamlines the query creation process by reducing the need to manually write complex SQL syntax.

How it Works

By entering a natural language instruction or request in the Message Field, the AI interprets your message and generates a corresponding SQL query. This is especially useful for users who may not be familiar with SQL syntax or need to create queries quickly.

Message Field

The Message Field allows you to input a plain text instruction describing the type of query you need. The AI uses this input to generate a query based on the provided table's context (e.g., columns, data types).

  • Example 1: You want to retrieve all employees whose age is greater than 30. You would enter:

    • Message: "Generate a query to select all employees whose age is greater than 30."
    • AI Output:
      SELECT * FROM employees WHERE age > 30;
  • Example 2: You want to fetch employee data for employees hired after 2020. You would enter:

    • Message: "Generate a query to find all employees hired after January 1, 2020."
    • AI Output:
      SELECT * FROM employees WHERE hire_date > '2020-01-01';

Table Context

The Table Context dropdown allows you to select a table from the currently connected database. This context is crucial because it provides the AI with the schema information of the selected table, such as column names and their data types. The AI uses this schema to ensure that the queries generated are accurate and match the table structure.

  • Example: If you select the employees table in the Table Context, the AI will generate queries based on the columns and data types defined in this table, such as id, name, age, and hire_date.

This feature ensures that the AI-generated query aligns with the selected table and prevents errors related to invalid column names or incorrect data types.

Similar Query

The Similar Query dropdown allows you to select existing queries that are similar to the one you are creating. This can help you reference or reuse previous queries, ensuring consistency and saving time.

  • Example: If you previously created a query like GetActiveEmployees, you can select it from the Similar Query dropdown and use it as a reference to build a new query with slight modifications.

Send Button

Once you've provided a message in the Message Field, you can click the Send button to instruct the AI to generate a query based on your input. The AI will process the request and generate an SQL query that matches the intent of your instruction.

  • Example: You enter the message "Generate a query to fetch all active employees." After clicking the Send button, the AI might generate the following query:
    SELECT * FROM employees WHERE is_active = true;
  1. Name Field:

    • The top-left Name field allows users to assign a meaningful name to the SQL query. Naming queries helps keep them organized and easy to reference later.
    • Example: You might name your query GetActiveEmployees, UpdateEmployeeSalary, etc.
  2. Connection:

    • The Connection dropdown allows users to select the database they want to execute the query on. In the image, the connection is set to EmployeDetailsdbDevelopment (MySQL).
    • This ensures that the query is targeted to the correct database instance.
  3. Query Type Tabs:

    • These tabs enable you to select the SQL operation type you wish to perform:
      • Select: Used for creating SELECT queries to retrieve data.
      • Insert: Used for creating INSERT queries to add new records.
      • Update: Used for creating UPDATE queries to modify existing records.
      • Delete: Used for creating DELETE queries to remove records from the database.
  4. Code Editor:

    • The code editor is where users can write or edit their SQL queries. The editor supports syntax highlighting for better readability.
    • Example of a SELECT query:
    SELECT * FROM employees WHERE department = 'HR';
  5. Params:

    • Clicking the Params button opens a menu where users can define parameters for dynamic queries. Parameters allow you to write queries that can be reused with different input values.
    • Example: You could write a parameterized query like
      SELECT * FROM employees WHERE id = :employee_id;

    where employee_id would be defined as a parameter.

  6. Save/Cancel:

    • After finishing the query, click Save to store it for future use or execution.
    • Click Cancel if you want to discard the changes or abort the query creation process.

Summary

The New SQL Query interface provides a robust environment for users to create, manage, and execute SQL queries. The interface supports multiple query types (SELECT, INSERT, UPDATE, DELETE) and includes helpful features like AI-assisted query generation and parameter support. By selecting the appropriate table context and reusing similar queries, users can streamline their query creation process. With intuitive controls and a clear layout, this tool enhances the experience of working with SQL queries, making it accessible to both beginners and advanced users alike.