{"id":974,"date":"2020-05-02T09:38:54","date_gmt":"2020-05-02T09:38:54","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/05\/02\/run-direct-sql-query-in-magento-2\/"},"modified":"2026-04-22T15:15:48","modified_gmt":"2026-04-22T09:45:48","slug":"run-direct-sql-query-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/run-direct-sql-query-in-magento-2\/","title":{"rendered":"Our Guide to Run Direct SQL Query in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The developers use models to access as well as modify the data in the Magento 2 database. However, sometimes, the project requirements demand communicating with the database using custom SQL queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post helps to\u00a0run a direct SQL query in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Also Check:<\/strong>&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/read-csv-file-using-root-script-in-magento-2\/\">Read CSV File Using Root Script in Magento 2<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Learn the easy method to write, execute\/run the direct SQL query as shown below:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to run direct SQL Query in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace [Vendor]\\[Module]\\Helper;\nuse Magento\\Framework\\App\\Helper\\AbstractHelper;\nuse Magento\\Framework\\App\\Helper\\Context;\nuse Magento\\Framework\\App\\ResourceConnection;\nuse Magento\\Framework\\App\\ObjectManager;\nclass Data extends AbstractHelper\n{\n    protected $resourceConnection;\n    public function __construct(Context $context, ResourceConnection $resourceConnection)\n    {\n        $this->resourceConnection = $resourceConnection;\n        parent::__construct($context);\n    }\n    public function runSqlQuery($table)\n    {\n        $connection = $this->resourceConnection->getConnection();\n        \/\/ $table is table name\n        $table = $connection->getTableName('my_custom_table');\n        \/\/For Select query\n        $query = \"Select * FROM \" . $table;\n        $result = $connection->fetchAll($query);\n        $this->_logger->log(print_r($result, true));\n        $id = 2;\n        $query = \"SELECT * FROM `\" . $table . \"` WHERE id = $id \";\n        $result1 = $connection->fetchAll($query);\n        $this->_logger->log(print_r($result1, true));\n        \/\/For Insert query\n        $tableColumn = ['id', 'name', 'age'];\n        $tableData[] = [5, 'xyz', '20'];\n        $connection->insertArray($table, $tableColumn, $tableData);\n        $query = \"INSERT INTO `\" . $table . \"`(`id`, `name`, `age`) VALUES (7,'mtm',33)\";\n        $connection->query($query);\n        \/\/ For Update query\n        $id = 1;\n        $query = \"UPDATE `\" . $table . \"` SET `name`= 'test' WHERE id = $id \";\n        $connection->query($query);\n        $query1 = \"UPDATE `\" . $table . \"` SET `name`= 'test', `age` = 14 WHERE id = $id \";\n        $connection->query($query1);\n        \/\/ For Delete query\n        $id = 1;\n        $query = \"DELETE FROM `\" . $table . \"` WHERE id = $id \";\n        $connection->query($query);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That was all about Magento 2 direct SQL query. In case you are required to run regular SQL queries on your online store to fetch custom data, you can use&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-2-sql-report-builder.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 SQL Report Builder<\/a>&nbsp;extension.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the post with the Magento (Adobe Commerce) community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The developers use models to access as well as modify the data in the Magento 2 database. However, sometimes, the project requirements demand communicating with&#8230;<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-974","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=974"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/974\/revisions"}],"predecessor-version":[{"id":26357,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/974\/revisions\/26357"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}