How can I write an SQL query by using the NOT CONTAINS keyword?

200    Asked by DonnaChapman in Salesforce , Asked on Apr 18, 2024

I am working as a database administrator for a particular retail company. The company wants to retrieve a list of products that do not contain specific keywords in their description. How can I write an SQL query by using the NOT CONTAINS keyword to fill this requirement? 

Answered by Dominic Poole

 In the context of Salesforce, here is how you can write an SQL query for your particular Objective:-


Let us assume that you have a “product” table with columns “product I’d” “product name “ and “description”. You want to retrieve the product which do not have the keyword “out of stock” in their description.

First, let’s create a sample “product “ table and insert some data:-

CREATE TABLE products (
    Product_id INT PRIMARY KEY,
    Product_name VARCHAR(50),
    Description VARCHAR(255)
);

INSERT INTO products (product_id, product_name, description)

VALUES

    (1, ‘Product A’, ‘This is a description for Product A’),

    (2, ‘Product B’, ‘Product B is currently out of stock’),

    (3, ‘Product C’, ‘Product C is available for purchase’);

Now you can write the SQL query to retrieve the product that does not contains the keyword out of stock in their description:-

SELECT *
FROM products
WHERE description NOT LIKE ‘%out of stock%’;

Here is the example given of java programming language which would help in implementing the SQL query and process the results:-

Import java.sql.Connection;
Import java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import java.sql.ResultSet;
Import java.sql.SQLException;
Public class ProductDAO {
    Private static final String DB_URL = “jdbc:mysql://localhost:3306/your_database”;
    Private static final String DB_USER = “your_username”;
    Private static final String DB_PASSWORD = “your_password”;
    Public void retrieveProductsNotContainingKeyword(String keyword) {
        Try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            String sqlQuery = “SELECT * FROM products WHERE description NOT LIKE ?”;
            Try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
                preparedStatement.setString(1, “%” + keyword + “%”);
                try (ResultSet resultSet = preparedStatement.executeQuery()) {
                    while (resultSet.next()) {
                        int productId = resultSet.getInt(“product_id”);
                        String productName = resultSet.getString(“product_name”);
                        String description = resultSet.getString(“description”);
                        // Process the retrieved data (e.g., display, store in Java objects)
                        System.out.println(“Product ID: “ + productId);
                        System.out.println(“Product Name: “ + productName);
                        System.out.println(“Description: “ + description);
                        System.out.println();
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    Public static void main(String[] args) {
        ProductDAO productDAO = new ProductDAO();
        productDAO.retrieveProductsNotContainingKeyword(“out of stock”);
    }
}


Your Answer

Interviews

Parent Categories