In the Using the Mozilla SQL Parser — Part 1 article on Mozilla SQL Parser, we covered how the moz-sql-parser package can be used to parse an SQL Select statement into a JSONified structure. In this part, we will cover how to extract names of the tables mentioned in the ‘from’ clause of the query.

The SQL we are using is

SELECT s_name from student where s_id in2
(select s_id from student_course where c_id in3
(select c_id from course where c_name = 'DSA'4
and c_name = 'dbms' or c_name = 'algorithm'));

To parse this query, we use

parsed_query = parse(query)

where, parse is a method provided by the moz_sql_parser.

After parsing, the module generates a JSONified structure, as given below.

JSON

#python #sql

Using the Mozilla SQL Parser
5.45 GEEK