Project: StarRocks (App Definition and Development / Database)
File: TPC-H_Benchmarking.md

displayed_sidebar: "English" TPC-H is a decision support benchmark developed by the Transaction Processing Performance Council (TPC). It consists of a suite of business oriented ad-hoc queries and concurrent data modifications. TPC-H can be used to build models based on real production environments to simulate the data warehouse of a sales system. This test uses eight tables with a data size ranging from 1 GB to 3 TB. A total of 22 queries are tested and the main performance metrics are the response time of each query, which is the duration between the time a query is submitted to the time the result is returned. We perform a test on 22 queries against a TPC-H 100 GB dataset. The following figure shows the test result. In the test, StarRocks queries data from both its native storage and Hive external tables. StarRocks and Trino query the same copy of data from Hive external tables. Data is ZLIB-compressed and stored in the ORC format. The latency for StarRocks to query data from its native storage is 21s, that for StarRocks to query Hive external tables is 92s, and that for Trino to query Hive external tables is as long as 307s. | Machine | 3 cloud hosts | | -- | - | | CPU | 16-core | | Memory | 64 GB | | Network bandwidth | 5 Gbit/s | | Disk | System disk: ESSD 40 GB Data disk: ESSD 100 GB | Kernel version: Linux 3.10.0-1127.13.1.el7.x86_64 OS version: CentOS Linux released 7.8.2003 Software version: StarRocks 2.1, Trino-357, Hive-3.1.2 StarRocks FE can be separately deployed or hybrid deployed with BEs, which does not affect the test results. The test uses a TPC-H 100 GB dataset. | Table | Records | Data size after compression on StarRocks | | -- | -- | - | | customer | 15 million | 2.4 GB | | lineitem | 600 million | 75 GB | | nation | 25 | 2.2 KB | | orders | 150 million | 16 GB | | part | 200 million | 2.4 GB | | partsupp | 800 million | 11.6 GB | | region | 5 | 389 B | | supplier | 1 million |

GB | ```SQL --Q1 select l_returnflag, l_linestatus, sum(lquantity) as sumqty, sum(lextendedprice) as sumbase_price, sum(lextendedprice * (1 - ldiscount)) as sumdiscprice, sum(lextendedprice * (1 - ldiscount) * (1 + ltax)) as sumcharge, avg(lquantity) as avgqty, avg(lextendedprice) as avgprice, avg(ldiscount) as avgdisc, count(*) as count_order from lineitem where l_shipdate <= date '1998-12-01' group by lreturnflag, llinestatus order by lreturnflag, llinestatus; --Q2 select sacctbal, sname, nname, ppartkey, pmfgr, saddress, sphone, scomment from part, partsupp, supplier, nation, region where ppartkey = pspartkey and ssuppkey = pssuppkey and p_size = 15 and p_type like '%BRASS' and snationkey = nnationkey and nregionkey = rregionkey and r_name = 'EUROPE' and ps_supplycost = ( select min(ps_supplycost) from partsupp, supplier, nation, region where ppartkey = pspartkey and ssuppkey = pssuppkey and snationkey = nnationkey and nregionkey = rregionkey and r_name = 'EUROPE' ) order by sacctbal desc, nname, sname, ppartkey; --Q3 select lorderkey, sum(lextendedprice * (1 - ldiscount)) as revenue, oorderdate, o_shippriority from customer, orders, lineitem where c_mktsegment = 'BUILDING' and ccustkey = ocustkey and lorderkey = oorderkey and o_orderdate < date '1995-03-15' and l_shipdate > date '1995-03-15' group by lorderkey, oorderdate, o_shippriority order by revenue desc, o_orderdate; --Q4 select oorderpriority, count(*) as ordercount from orders where o_orderdate >= date '1993-07-01' and o_orderdate < date '1993-07-01' + interval '3' month and exists ( select * from lineitem where lorderkey = oorderkey and lcommitdate < lreceiptdate ) group by o_orderpriority order by o_orderpriority; --Q5 select nname, sum(lextendedprice * (1 - l_discount)) as revenue from customer, orders, lineitem, supplier, nation, region where ccustkey = ocustkey and lorderkey = oorderkey and lsuppkey = ssuppkey and cnationkey = snationkey and snationkey = nnationkey and nregionkey = rregionkey and r_name = 'ASIA' and o_orderdate >= date '1994-01-01' and o_orderdate < date '1994-01-01' + interval '1' year group by n_name order by revenue desc; --Q6 select sum(lextendedprice * ldiscount) as revenue from lineitem where l_shipdate >= date '1994-01-01' and l_shipdate < date '1994-01-01' + interval '1' year and l_discount between .06 - 0.01 and .06 + 0.01 and l_quantity < 24; --Q7 select suppnation, custnation, l_year, sum(volume) as revenue from ( select n1.nname as suppnation, n2.nname as custnation, extract(year from lshipdate) as lyear, lextendedprice * (1 - ldiscount) as volume from supplier, lineitem, orders, customer, nation n1, nation n2 where ssuppkey = lsuppkey and oorderkey = lorderkey and ccustkey = ocustkey and snationkey = n1.nnationkey and cnationkey = n2.nnationkey and ( (n1.nname = 'FRANCE' and n2.nname = 'GERMANY') or (n1.nname = 'GERMANY' and n2.nname = 'FRANCE') ) and l_shipdate between date '1995-01-01' and date '1996-12-31' ) as shipping group by suppnation, custnation, l_year order by suppnation, custnation, l_year; --Q8 select oyear, sum(case when nation = 'BRAZIL' then volume else 0 end) / sum(volume) as mktshare from ( select extract(year from oorderdate) as oyear, lextendedprice * (1 - ldiscount) as volume, n2.n_name as nation from part, supplier, lineitem, orders, customer, nation n1, nation n2, region where ppartkey = lpartkey and ssuppkey = lsuppkey and lorderkey = oorderkey and ocustkey = ccustkey and cnationkey = n1.nnationkey and n1.nregionkey = rregionkey and r_name = 'AMERICA' and snationkey = n2.nnationkey and o_orderdate between date '1995-01-01' and date '1996-12-31' and p_type = 'ECONOMY ANODIZED STEEL' ) as all_nations group by o_year order by o_year; --Q9 select nation, oyear, sum(amount) as sumprofit from ( select n_name as nation, extract(year from oorderdate) as oyear, lextendedprice * (1 - ldiscount) - pssupplycost * lquantity as amount from part, supplier, lineitem, partsupp, orders, nation where ssuppkey = lsuppkey and pssuppkey = lsuppkey and pspartkey = lpartkey and ppartkey = lpartkey and oorderkey = lorderkey and snationkey = nnationkey and p_name like '%green%' ) as profit group by nation, o_year order by nation, o_year desc; --Q10 select ccustkey, cname, sum(lextendedprice * (1 - ldiscount)) as revenue, cacctbal, nname, caddress, cphone, c_comment from customer, orders, lineitem, nation where ccustkey = ocustkey and lorderkey = oorderkey and o_orderdate >= date '1993-10-01' and o_orderdate < date '1993-12-01' and l_returnflag = 'R' and cnationkey = nnationkey group by ccustkey, cname, cacctbal, cphone, nname, caddress, c_comment order by revenue desc; --Q11 select pspartkey, sum(pssupplycost * ps_availqty) as value from partsupp, supplier, nation where pssuppkey = ssuppkey and snationkey = nnationkey and n_name = 'GERMANY' group by ps_partkey having sum(pssupplycost * psavailqty) > ( select sum(pssupplycost * psavailqty) *

from partsupp, supplier, nation where pssuppkey = ssuppkey and snationkey = nnationkey and n_name = 'GERMANY' ) order by value desc; --Q12 select l_shipmode, sum(case when oorderpriority = '1-URGENT' or oorderpriority = '2-HIGH' then 1 else 0 end) as highlinecount, sum(case when oorderpriority <> '1-URGENT' and oorderpriority <> '2-HIGH' then 1 else 0 end) as lowlinecount from orders, lineitem where oorderkey = lorderkey and l_shipmode in ('MAIL', 'SHIP') and lcommitdate < lreceiptdate and lshipdate < lcommitdate and l_receiptdate >= date '1994-01-01' and l_receiptdate < date '1994-01-01' + interval '1' year group by l_shipmode order by l_shipmode; --Q13 select c_count, count(*) as custdist from (select ccustkey,count(oorderkey) as c_count from customer left outer join orders on ccustkey = ocustkey and o_comment not like '%special%requests%' group by c_custkey ) as c_orders group by c_count order by custdist desc, c_count desc; --Q14 select 100.00 sum(case when p_type like 'PROMO%' then l_extendedprice (1 - ldiscount) else 0 end) / sum(lextendedprice * (1 - ldiscount)) as promorevenue from lineitem, part where lpartkey = ppartkey and l_shipdate >= date '1995-09-01' and l_shipdate < date '1995-09-01' + interval '1' month; --Q15 select ssuppkey, sname, saddress, sphone, total_revenue from supplier, revenue0 where ssuppkey = supplierno and total_revenue = ( select max(total_revenue) from revenue0 ) order by s_suppkey; --Q16 select pbrand, ptype, psize, count(distinct pssuppkey) as supplier_cnt from partsupp, part where ppartkey = pspartkey and p_brand <> 'Brand#45' and p_type not like 'MEDIUM POLISHED%' and p_size in (49, 14, 23, 45, 19, 3, 36, 9) and ps_suppkey not in ( select ssuppkey from supplier where scomment like '%Customer%Complaints%' ) group by pbrand, ptype, p_size order by suppliercnt desc, pbrand, ptype, psize; --Q17 select sum(lextendedprice) / 7.0 as avgyearly from lineitem, part where ppartkey = lpartkey and p_brand = 'Brand#23' and p_container = 'MED BOX' and l_quantity < ( select 0.2 * avg(lquantity) from lineitem where lpartkey = p_partkey ); --Q18 select cname, ccustkey, oorderkey, oorderdate, ototalprice, sum(lquantity) from customer, orders, lineitem where o_orderkey in ( select l_orderkey from lineitem group by l_orderkey having sum(l_quantity) > 300 ) and ccustkey = ocustkey and oorderkey = lorderkey group by cname, ccustkey, oorderkey, oorderdate, o_totalprice order by ototalprice desc, oorderdate; --Q19 select sum(lextendedprice* (1 - ldiscount)) as revenue from lineitem, part where ( ppartkey = lpartkey and p_brand = 'Brand#12' and p_container in ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG') and lquantity >= 1 and lquantity <= 1 + 10 and p_size between 1 and 5 and l_shipmode in ('AIR', 'AIR REG') and l_shipinstruct = 'DELIVER IN PERSON' ) or ( ppartkey = lpartkey and p_brand = 'Brand#23' and p_container in ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK') and lquantity >= 10 and lquantity <= 10 + 10 and p_size between 1 and 10 and l_shipmode in ('AIR', 'AIR REG') and l_shipinstruct = 'DELIVER IN PERSON' ) or ( ppartkey = lpartkey and p_brand

'Brand#34' and p_container in ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG') and lquantity >= 20 and lquantity <= 20 + 10 and p_size between 1 and 15 and l_shipmode in ('AIR', 'AIR REG') and l_shipinstruct = 'DELIVER IN PERSON' ); --Q20 select sname, saddress from supplier, nation where s_suppkey in ( select ps_suppkey from partsupp where ps_partkey in ( select p_partkey from part where p_name like 'forest%' ) and ps_availqty > ( select 0.5 * sum(l_quantity) from lineitem where lpartkey = pspartkey and lsuppkey = pssuppkey and l_shipdate >= date '1994-01-01' and l_shipdate < date '1994-01-01' + interval '1' year ) ) and snationkey = nnationkey and n_name = 'CANADA' order by s_name; --Q21 select s_name, count(*) as numwait from supplier, lineitem l1, orders, nation where ssuppkey = l1.lsuppkey and oorderkey = l1.lorderkey and o_orderstatus = 'F' and l1.lreceiptdate > l1.lcommitdate and exists ( select * from lineitem l2 where l2.lorderkey = l1.lorderkey and l2.lsuppkey <> l1.lsuppkey ) and not exists ( select * from lineitem l3 where l3.lorderkey = l1.lorderkey and l3.lsuppkey <> l1.lsuppkey and l3.lreceiptdate > l3.lcommitdate ) and snationkey = nnationkey and n_name = 'SAUDI ARABIA' group by s_name order by numwait desc, s_name; --Q22 select cntrycode, count(*) as numcust, sum(c_acctbal) as totacctbal from ( select substr(c_phone, 1, 2) as cntrycode, c_acctbal from customer where substr(c_phone, 1, 2) in ('13', '31', '23', '29', '30', '18', '17') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('13', '31', '23', '29', '30', '18', '17') ) and not exists ( select from orders where ocustkey = ccustkey ) ) as custsale group by cntrycode order by cntrycode; ``` | Query | StarRocks-native (ms) | StarRocks-Hive external (ms) | Trino (ms) | | | - | -- | -- | | q1 | 1291 | 5349 | 14350 | | q2 | 257 | 940 | 4075 | | q3 | 690 | 2585 | 7615 | | q4 | 529 | 2514 | 9385 | | q5 | 1142 | 5844 | 15350 | | q6 | 115 | 2936 | 4045 | | q7 | 1435 | 5128 | 8760 | | q8 | 855 | 5989 | 9535 | | q9 | 3028 | 5507 | 27310 | | q10 | 1381 | 3654 | 6560 | | q11 | 265 | 691 | 2090 | | q12 | 387 | 3827 | 7635 | | q13 | 2165 | 3733 | 14530 | | q14 | 184 | 3824 | 4355 | | q15 | 354 | 7321 | 8975 | | q16 | 240 | 1029 | 2265 | | q17 | 753 | 5011 | 46405 | | q18 | 2909 | 7032 | 30975 | | q19 | 702 | 3905 | 7315 | | q20 | 963 | 4623 | 12500 | | q21 | 870 | 10016 | 58340 | | q22 | 545 | 991 | 5605 | | sum | 21060 | 92449 | 307975 | Download the tpch-poc toolkit and compile it. ```Bash wget https://starrocks-public.oss-cn-zhangjiakou.aliyuncs.com/tpch-poc-0.1.2.zip unzip tpch-poc-0.1.2.zip cd tpch-poc-0.1.2 cd benchmark ``` The related tools are stored in the `bin` directory and you can initiate operations in the home directory. Generate data. ```Bash ./bin/gendata/gen-tpch.sh 100 data100 ``` Modify the configuration file `conf/starrocks.conf` and specify the cluster address. ```SQL mysql_host: 192.168.1.1 mysql_port: 9030 mysql_user: root mysql_password: database: tpch http_port: 8030 beheartbeatport: 9050 broker_port: 8000 parallel_num: 8 ``` Run the following script to create a table: ```SQL ./bin/createdbtable.sh ddl_100 ``` The table has been created and the default bucket number has been

You can delete the previous table and re-plan the bucket number based on your cluster size and node configuration, which helps achieve better test results. ```SQL drop table if exists customer; CREATE TABLE customer ( c_custkey int NOT NULL, c_name VARCHAR(25) NOT NULL, c_address VARCHAR(40) NOT NULL, c_nationkey int NOT NULL, c_phone VARCHAR(15) NOT NULL, c_acctbal decimal(15, 2) NOT NULL, c_mktsegment VARCHAR(10) NOT NULL, c_comment VARCHAR(117) NOT NULL )ENGINE=OLAP DUPLICATE KEY(`c_custkey`) COMMENT "OLAP" DISTRIBUTED BY HASH(`c_custkey`) BUCKETS 24 PROPERTIES ( "replication_num" = "1", "storage_format" = "DEFAULT" ); drop table if exists lineitem; CREATE TABLE lineitem ( l_shipdate DATE NOT NULL, l_orderkey int NOT NULL, l_linenumber int not null, l_partkey int NOT NULL, l_suppkey int not null, l_quantity decimal(15, 2) NOT NULL, l_extendedprice decimal(15, 2) NOT NULL, l_discount decimal(15, 2) NOT NULL, l_tax decimal(15, 2) NOT NULL, l_returnflag VARCHAR(1) NOT NULL, l_linestatus VARCHAR(1) NOT NULL, l_commitdate DATE NOT NULL, l_receiptdate DATE NOT NULL, l_shipinstruct VARCHAR(25) NOT NULL, l_shipmode VARCHAR(10) NOT NULL, l_comment VARCHAR(44) NOT NULL )ENGINE=OLAP DUPLICATE KEY(`lshipdate`, `lorderkey`) COMMENT "OLAP" DISTRIBUTED BY HASH(`l_orderkey`) BUCKETS 96 PROPERTIES ( "replication_num" = "1", "storage_format" = "DEFAULT", "colocate_with" = "tpch2" ); drop table if exists nation; CREATE TABLE `nation` ( `n_nationkey` int(11) NOT NULL, `n_name` varchar(25) NOT NULL, `n_regionkey` int(11) NOT NULL, `n_comment` varchar(152) NULL ) ENGINE=OLAP DUPLICATE KEY(`N_NATIONKEY`) COMMENT "OLAP" DISTRIBUTED BY HASH(`N_NATIONKEY`) BUCKETS 1 PROPERTIES ( "replication_num" = "3", "storage_format" = "DEFAULT" ); drop table if exists orders; CREATE TABLE orders ( o_orderkey int NOT NULL, o_orderdate DATE NOT NULL, o_custkey int NOT NULL, o_orderstatus VARCHAR(1) NOT NULL, o_totalprice decimal(15, 2) NOT NULL, o_orderpriority VARCHAR(15) NOT NULL, o_clerk VARCHAR(15) NOT NULL, o_shippriority int NOT NULL, o_comment VARCHAR(79) NOT NULL )ENGINE=OLAP DUPLICATE KEY(`oorderkey`, `oorderdate`) COMMENT "OLAP" DISTRIBUTED BY HASH(`o_orderkey`) BUCKETS 96 PROPERTIES ( "replication_num" = "1", "storage_format" = "DEFAULT", "colocate_with" = "tpch2" ); drop table if exists part; CREATE TABLE part ( p_partkey int NOT NULL, p_name VARCHAR(55) NOT NULL, p_mfgr VARCHAR(25) NOT NULL, p_brand VARCHAR(10) NOT NULL, p_type VARCHAR(25) NOT NULL, p_size int NOT NULL, p_container VARCHAR(10) NOT NULL, p_retailprice decimal(15, 2) NOT NULL, p_comment VARCHAR(23) NOT NULL )ENGINE=OLAP DUPLICATE KEY(`p_partkey`) COMMENT "OLAP" DISTRIBUTED BY HASH(`p_partkey`) BUCKETS 24 PROPERTIES ( "replication_num" = "1", "storage_format" = "DEFAULT", "colocate_with" = "tpch2p" ); drop table if exists partsupp; CREATE TABLE partsupp ( ps_partkey int NOT NULL, ps_suppkey int NOT NULL, ps_availqty int NOT NULL, ps_supplycost decimal(15, 2) NOT NULL, ps_comment VARCHAR(199) NOT NULL )ENGINE=OLAP DUPLICATE KEY(`ps_partkey`) COMMENT "OLAP" DISTRIBUTED BY HASH(`ps_partkey`) BUCKETS 24 PROPERTIES ( "replication_num" = "1", "storage_format" = "DEFAULT", "colocate_with" = "tpch2p" ); drop table if exists region; CREATE TABLE region ( r_regionkey int NOT NULL, r_name VARCHAR(25) NOT NULL, r_comment VARCHAR(152) )ENGINE=OLAP DUPLICATE KEY(`r_regionkey`) COMMENT "OLAP" DISTRIBUTED BY HASH(`r_regionkey`) BUCKETS 1 PROPERTIES ( "replication_num" = "3", "storage_format" = "DEFAULT" ); drop table if exists supplier; CREATE TABLE supplier ( s_suppkey int NOT NULL, s_name VARCHAR(25) NOT NULL, s_address VARCHAR(40) NOT NULL, s_nationkey int NOT NULL, s_phone VARCHAR(15) NOT NULL, s_acctbal decimal(15, 2) NOT NULL, s_comment VARCHAR(101) NOT NULL )ENGINE=OLAP DUPLICATE KEY(`s_suppkey`) COMMENT "OLAP" DISTRIBUTED BY HASH(`s_suppkey`) BUCKETS 12 PROPERTIES ( "replication_num" = "1", "storage_format" = "DEFAULT" ); drop view if exists revenue0; create view revenue0 (supplierno, totalrevenue) as select l_suppkey, sum(lextendedprice * (1 - ldiscount)) from lineitem where l_shipdate >= date '1996-01-01' and l_shipdate < date '1996-01-01' + interval '3' month group by l_suppkey; ``` ```Python

data100 ``` This step is optional. We recommend that full statistical information be collected to generate better test results. Run the following commands after you connect to the StarRocks cluster: ```SQL ANALYZE FULL TABLE customer; ANALYZE FULL TABLE lineitem; ANALYZE FULL TABLE nation; ANALYZE FULL TABLE orders; ANALYZE FULL TABLE part; ANALYZE FULL TABLE partsupp; ANALYZE FULL TABLE region; ANALYZE FULL TABLE supplier; ``` ```Python ./bin/benchmark.sh -p -d tpch ``` Create table schema in Hive. The storage format is ORC. ```SQL create database tpchhiveorc; use tpchhiveorc; --Create a table named customer. CREATE TABLE `customer`( `c_custkey` int, `c_name` varchar(25), `c_address` varchar(40), `c_nationkey` int, `c_phone` varchar(15), `c_acctbal` decimal(15,2), `c_mktsegment` varchar(10), `c_comment` varchar(117)) ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/hive/warehouse/tpchhiveorc.db/customer'; --Create a table named lineitem. CREATE TABLE `lineitem`( `l_orderkey` bigint, `l_partkey` int, `l_suppkey` int, `l_linenumber` int, `l_quantity` decimal(15,2), `l_extendedprice` decimal(15,2), `l_discount` decimal(15,2), `l_tax` decimal(15,2), `l_returnflag` varchar(1), `l_linestatus` varchar(1), `l_shipdate` date, `l_commitdate` date, `l_receiptdate` date, `l_shipinstruct` varchar(25), `l_shipmode` varchar(10), `l_comment` varchar(44)) ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/hive/warehouse/tpchhiveorc.db/lineitem'; --Create a table named nation. CREATE TABLE `nation`( `n_nationkey` int, `n_name` varchar(25), `n_regionkey` int, `n_comment` varchar(152)) ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/hive/warehouse/tpchhiveorc.db/nation'; --Create a table named orders. CREATE TABLE `orders`( `o_orderkey` bigint, `o_custkey` int, `o_orderstatus` varchar(1), `o_totalprice` decimal(15,2), `o_orderdate` date, `o_orderpriority` varchar(15), `o_clerk` varchar(15), `o_shippriority` int, `o_comment` varchar(79)) ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/hive/warehouse/tpchhiveorc.db/orders'; --Create a table named part. CREATE TABLE `part`( `p_partkey` int, `p_name` varchar(55), `p_mfgr` varchar(25), `p_brand` varchar(10), `p_type` varchar(25), `p_size` int, `p_container` varchar(10), `p_retailprice` decimal(15,2), `p_comment` varchar(23)) ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/hive/warehouse/tpchhiveorc.db/part'; --Create a table named partsupp. CREATE TABLE `partsupp`( `ps_partkey` int, `ps_suppkey` int, `ps_availqty` int, `ps_supplycost` decimal(15,2), `ps_comment` varchar(199)) ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/hive/warehouse/tpchhiveorc.db/partsupp'; --Create a table named region. CREATE TABLE `region`( `r_regionkey` int, `r_name` varchar(25), `r_comment` varchar(152)) ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/hive/warehouse/tpchhiveorc.db/region'; --Create a table named supplier. CREATE TABLE `supplier`( `s_suppkey` int, `s_name` varchar(25), `s_address` varchar(40), `s_nationkey` int, `s_phone` varchar(15), `s_acctbal` decimal(15,2), `s_comment` varchar(101)) ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/hive/warehouse/tpchhiveorc.db/supplier'; ``` Create external tables in StarRocks. ```SQL create database if not exists tpch_sr; use tpch_sr; CREATE EXTERNAL RESOURCE "hive_test" PROPERTIES ( "type" = "hive", "hive.metastore.uris" =

); CREATE EXTERNAL TABLE IF NOT EXISTS nation ( n_nationkey int, n_name VARCHAR(25), n_regionkey int, n_comment varchar(152)) ENGINE=hive properties ( "resource" = "hive_test", "table" = "nation", "database" = "tpchhiveorc" ); CREATE EXTERNAL TABLE IF NOT EXISTS customer ( c_custkey INTEGER, c_name VARCHAR(25), c_address VARCHAR(40), c_nationkey INTEGER, c_phone VARCHAR(15), c_acctbal DECIMAL(15,2), c_mktsegment VARCHAR(10), c_comment VARCHAR(117)) ENGINE=hive properties ( "resource" = "hive_test", "table" = "customer", "database" = "tpchhiveorc" ); CREATE EXTERNAL TABLE IF NOT EXISTS lineitem ( l_orderkey BIGINT, l_partkey INTEGER, l_suppkey INTEGER, l_linenumber INTEGER, l_quantity DECIMAL(15,2), l_extendedprice DECIMAL(15,2), l_discount DECIMAL(15,2), l_tax DECIMAL(15,2), l_returnflag VARCHAR(1), l_linestatus VARCHAR(1), l_shipdate DATE, l_commitdate DATE, l_receiptdate DATE, l_shipinstruct VARCHAR(25), l_shipmode VARCHAR(10), l_comment VARCHAR(44)) ENGINE=hive properties ( "resource" = "hive_test", "table" = "lineitem", "database" = "tpchhiveorc" ); CREATE EXTERNAL TABLE IF NOT EXISTS orders ( o_orderkey BIGINT, o_custkey INTEGER, o_orderstatus VARCHAR(1), o_totalprice DECIMAL(15,2), o_orderdate DATE, o_orderpriority VARCHAR(15), o_clerk VARCHAR(15), o_shippriority INTEGER, o_comment VARCHAR(79)) ENGINE=hive properties ( "resource" = "hive_test", "table" = "orders", "database" = "tpchhiveorc" ); CREATE EXTERNAL TABLE IF NOT EXISTS part ( p_partkey INTEGER, p_name VARCHAR(55), p_mfgr VARCHAR(25), p_brand VARCHAR(10), p_type VARCHAR(25), p_size INTEGER, p_container VARCHAR(10), p_retailprice DECIMAL(15,2), p_comment VARCHAR(23)) ENGINE=hive properties ( "resource" = "hive_test", "table" = "part", "database" = "tpchhiveorc" ); CREATE EXTERNAL TABLE IF NOT EXISTS partsupp ( ps_partkey INTEGER, ps_suppkey INTEGER, ps_availqty INTEGER, ps_supplycost DECIMAL(15,2), ps_comment VARCHAR(199)) ENGINE=hive properties ( "resource" = "hive_test", "table" = "partsupp", "database" = "tpchhiveorc" ); CREATE EXTERNAL TABLE IF NOT EXISTS region ( r_regionkey INTEGER, r_name VARCHAR(25), r_comment VARCHAR(152)) ENGINE=hive properties ( "resource" = "hive_test", "table" = "region", "database" = "tpchhiveorc" ); CREATE EXTERNAL TABLE IF NOT EXISTS supplier ( s_suppkey INTEGER, s_name VARCHAR(25), s_address VARCHAR(40), s_nationkey INTEGER, s_phone VARCHAR(15), s_acctbal DECIMAL(15,2), s_comment VARCHAR(101)) ENGINE=hive properties ( "resource" = "hive_test", "table" = "supplier", "database" = "tpchhiveorc" ); create view revenue0 (supplierno, totalrevenue) as select l_suppkey, sum(lextendedprice * (1 - ldiscount)) from lineitem where l_shipdate >= date '1996-01-01' and l_shipdate < date '1996-01-01' + interval '3' month group by l_suppkey; ``` Create Hive external tables in Hive. The table format is CSV. Upload CSV test data to the data storage directory on HDFS. In this example, the HDFS data storage path of the Hive external table is /user/tmp/csv/. ```SQL create database tpchhivecsv; use tpchhivecsv; --Create an external table named customer. CREATE EXTERNAL TABLE `customer`( `c_custkey` int, `c_name` varchar(25), `c_address` varchar(40), `c_nationkey` int, `c_phone` varchar(15), `c_acctbal` double, `c_mktsegment` varchar(10), `c_comment` varchar(117)) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/tmp/csv/customer_csv'; --Create an external table named lineitem. CREATE EXTERNAL TABLE `lineitem`( `l_orderkey` int, `l_partkey` int, `l_suppkey` int, `l_linenumber` int, `l_quantity` double, `l_extendedprice` double, `l_discount` double, `l_tax` double, `l_returnflag` varchar(1), `l_linestatus` varchar(1), `l_shipdate` date, `l_commitdate` date, `l_receiptdate` date, `l_shipinstruct` varchar(25), `l_shipmode` varchar(10), `l_comment` varchar(44)) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/tmp/csv/lineitem_csv'; --Create an external table named nation. CREATE EXTERNAL TABLE `nation`( `n_nationkey` int, `n_name` varchar(25), `n_regionkey` int, `n_comment` varchar(152)) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/tmp/csv/nation_csv'; --Create an external table named orders. CREATE EXTERNAL TABLE `orders`( `o_orderkey` int, `o_custkey` int, `o_orderstatus` varchar(1), `o_totalprice` double, `o_orderdate` date, `o_orderpriority` varchar(15), `o_clerk` varchar(15), `o_shippriority` int, `o_comment` varchar(79)) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/tmp/csv/orders_csv'; --Create an external table named part. CREATE EXTERNAL TABLE `part`( `p_partkey` int, `p_name` varchar(55), `p_mfgr` varchar(25), `p_brand` varchar(10), `p_type` varchar(25), `p_size` int, `p_container` varchar(10), `p_retailprice` double, `p_comment` varchar(23)) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/tmp/csv/part_csv'; --Create an external table named partsupp. CREATE EXTERNAL TABLE `partsupp`( `ps_partkey` int, `ps_suppkey` int, `ps_availqty` int, `ps_supplycost` double, `ps_comment` varchar(199)) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/tmp/csv/partsupp_csv'; --Create an external table named

CREATE EXTERNAL TABLE `region`( `r_regionkey` int, `r_name` varchar(25), `r_comment` varchar(152)) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/tmp/csv/region_csv'; --Create an external table named supplier. CREATE EXTERNAL TABLE `supplier`( `s_suppkey` int, `s_name` varchar(25), `s_address` varchar(40), `s_nationkey` int, `s_phone` varchar(15), `s_acctbal` double, `s_comment` varchar(101)) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 'field.delim'='|', 'serialization.format'='|') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 'hdfs://emr-header-1.cluster-49146:9000/user/tmp/csv/supplier_csv'; ``` Transfer CSV data in each Hive external table to a Hive table of the ORC format. This helps create Hive external tables of the ORC format in StarRocks and Trino for subsequent testing. ```SQL use tpchhivecsv; insert into tpchhiveorc.customer select * from customer; insert into tpchhiveorc.lineitem select * from lineitem; insert into tpchhiveorc.nation select * from nation; insert into tpchhiveorc.orders select * from orders; insert into tpchhiveorc.part select * from part; insert into tpchhiveorc.partsupp select * from partsupp; insert into tpchhiveorc.region select * from region; insert into tpchhiveorc.supplier select * from supplier; ``` ```SQL use tpch_sr; --Query system variables of current session. show variables; --Set the parallelism to 8. set parallelfragmentexecinstancenum = 8; --Q1 select l_returnflag, l_linestatus, sum(lquantity) as sumqty, sum(lextendedprice) as sumbase_price, sum(lextendedprice * (1 - ldiscount)) as sumdiscprice, sum(lextendedprice * (1 - ldiscount) * (1 + ltax)) as sumcharge, avg(lquantity) as avgqty, avg(lextendedprice) as avgprice, avg(ldiscount) as avgdisc, count(*) as count_order from lineitem where l_shipdate <= date '1998-12-01' group by lreturnflag, llinestatus order by lreturnflag, llinestatus; --Q2 select sacctbal, sname, nname, ppartkey, pmfgr, saddress, sphone, scomment from part, partsupp, supplier, nation, region where ppartkey = pspartkey and ssuppkey = pssuppkey and p_size = 15 and p_type like '%BRASS' and snationkey = nnationkey and nregionkey = rregionkey and r_name = 'EUROPE' and ps_supplycost = ( select min(ps_supplycost) from partsupp, supplier, nation, region where ppartkey = pspartkey and ssuppkey = pssuppkey and snationkey = nnationkey and nregionkey = rregionkey and r_name = 'EUROPE' ) order by sacctbal desc, nname, sname, ppartkey; --Q3 select lorderkey, sum(lextendedprice * (1 - ldiscount)) as revenue, oorderdate, o_shippriority from customer, orders, lineitem where c_mktsegment = 'BUILDING' and ccustkey = ocustkey and lorderkey = oorderkey and o_orderdate < date '1995-03-15' and l_shipdate > date '1995-03-15' group by lorderkey, oorderdate, o_shippriority order by revenue desc, o_orderdate; --Q4 select oorderpriority, count(*) as ordercount from orders where o_orderdate >= date '1993-07-01' and o_orderdate < date '1993-07-01' + interval '3' month and exists ( select * from lineitem where lorderkey = oorderkey and lcommitdate < lreceiptdate ) group by o_orderpriority order by o_orderpriority; --Q5 select nname, sum(lextendedprice * (1 - l_discount)) as revenue from customer, orders, lineitem, supplier, nation, region where ccustkey = ocustkey and lorderkey = oorderkey and lsuppkey = ssuppkey and cnationkey = snationkey and snationkey = nnationkey and nregionkey = rregionkey and r_name = 'ASIA' and o_orderdate >= date '1994-01-01' and o_orderdate < date '1994-01-01' + interval '1' year group by n_name order by revenue desc; --Q6 select sum(lextendedprice * ldiscount) as revenue from lineitem where l_shipdate >= date '1994-01-01' and l_shipdate < date '1994-01-01' + interval '1' year and l_discount between .06 - 0.01 and .06 + 0.01 and l_quantity < 24; --Q7 select suppnation, custnation, l_year, sum(volume) as revenue from ( select n1.nname as suppnation,

as custnation, extract(year from lshipdate) as lyear, lextendedprice * (1 - ldiscount) as volume from supplier, lineitem, orders, customer, nation n1, nation n2 where ssuppkey = lsuppkey and oorderkey = lorderkey and ccustkey = ocustkey and snationkey = n1.nnationkey and cnationkey = n2.nnationkey and ( (n1.nname = 'FRANCE' and n2.nname = 'GERMANY') or (n1.nname = 'GERMANY' and n2.nname = 'FRANCE') ) and l_shipdate between date '1995-01-01' and date '1996-12-31' ) as shipping group by suppnation, custnation, l_year order by suppnation, custnation, l_year; --Q8 select oyear, sum(case when nation = 'BRAZIL' then volume else 0 end) / sum(volume) as mktshare from ( select extract(year from oorderdate) as oyear, lextendedprice * (1 - ldiscount) as volume, n2.n_name as nation from part, supplier, lineitem, orders, customer, nation n1, nation n2, region where ppartkey = lpartkey and ssuppkey = lsuppkey and lorderkey = oorderkey and ocustkey = ccustkey and cnationkey = n1.nnationkey and n1.nregionkey = rregionkey and r_name = 'AMERICA' and snationkey = n2.nnationkey and o_orderdate between date '1995-01-01' and date '1996-12-31' and p_type = 'ECONOMY ANODIZED STEEL' ) as all_nations group by o_year order by o_year; --Q9 select nation, oyear, sum(amount) as sumprofit from ( select n_name as nation, extract(year from oorderdate) as oyear, lextendedprice * (1 - ldiscount) - pssupplycost * lquantity as amount from part, supplier, lineitem, partsupp, orders, nation where ssuppkey = lsuppkey and pssuppkey = lsuppkey and pspartkey = lpartkey and ppartkey = lpartkey and oorderkey = lorderkey and snationkey = nnationkey and p_name like '%green%' ) as profit group by nation, o_year order by nation, o_year desc; --Q10 select ccustkey, cname, sum(lextendedprice * (1 - ldiscount)) as revenue, cacctbal, nname, caddress, cphone, c_comment from customer, orders, lineitem, nation where ccustkey = ocustkey and lorderkey = oorderkey and o_orderdate >= date '1993-10-01' and o_orderdate < date '1993-12-01' and l_returnflag = 'R' and cnationkey = nnationkey group by ccustkey, cname, cacctbal, cphone, nname, caddress, c_comment order by revenue desc; --Q11 select pspartkey, sum(pssupplycost * ps_availqty) as value from partsupp, supplier, nation where pssuppkey = ssuppkey and snationkey = nnationkey and n_name = 'GERMANY' group by ps_partkey having sum(pssupplycost * psavailqty) > ( select sum(pssupplycost * psavailqty) * 0.0001000000 from partsupp, supplier, nation where pssuppkey = ssuppkey and snationkey = nnationkey and n_name = 'GERMANY' ) order by value desc; --Q12 select l_shipmode, sum(case when oorderpriority = '1-URGENT' or oorderpriority = '2-HIGH' then 1 else 0 end) as highlinecount, sum(case when oorderpriority <> '1-URGENT' and oorderpriority <> '2-HIGH' then 1 else 0 end) as lowlinecount from orders, lineitem where oorderkey = lorderkey and l_shipmode in ('MAIL', 'SHIP') and lcommitdate < lreceiptdate and lshipdate < lcommitdate and l_receiptdate >= date '1994-01-01' and l_receiptdate < date '1994-01-01' + interval '1' year group by l_shipmode order by l_shipmode; --Q13 select c_count, count(*) as custdist from (select ccustkey,count(oorderkey) as c_count from customer left outer join orders on ccustkey = ocustkey and o_comment not like '%special%requests%' group by c_custkey ) as c_orders group by c_count order by custdist desc, c_count desc; --Q14 select

sum(case when p_type like 'PROMO%' then l_extendedprice (1 - ldiscount) else 0 end) / sum(lextendedprice * (1 - ldiscount)) as promorevenue from lineitem, part where lpartkey = ppartkey and l_shipdate >= date '1995-09-01' and l_shipdate < date '1995-09-01' + interval '1' month; --Q15 select ssuppkey, sname, saddress, sphone, total_revenue from supplier, revenue0 where ssuppkey = supplierno and total_revenue = ( select max(total_revenue) from revenue0 ) order by s_suppkey; --Q16 select pbrand, ptype, psize, count(distinct pssuppkey) as supplier_cnt from partsupp, part where ppartkey = pspartkey and p_brand <> 'Brand#45' and p_type not like 'MEDIUM POLISHED%' and p_size in (49, 14, 23, 45, 19, 3, 36, 9) and ps_suppkey not in ( select ssuppkey from supplier where scomment like '%Customer%Complaints%' ) group by pbrand, ptype, p_size order by suppliercnt desc, pbrand, ptype, psize; --Q17 select sum(lextendedprice) / 7.0 as avgyearly from lineitem, part where ppartkey = lpartkey and p_brand = 'Brand#23' and p_container = 'MED BOX' and l_quantity < ( select 0.2 * avg(lquantity) from lineitem where lpartkey = p_partkey ); --Q18 select cname, ccustkey, oorderkey, oorderdate, ototalprice, sum(lquantity) from customer, orders, lineitem where o_orderkey in ( select l_orderkey from lineitem group by l_orderkey having sum(l_quantity) > 300 ) and ccustkey = ocustkey and oorderkey = lorderkey group by cname, ccustkey, oorderkey, oorderdate, o_totalprice order by ototalprice desc, oorderdate; --Q19 select sum(lextendedprice* (1 - ldiscount)) as revenue from lineitem, part where ( ppartkey = lpartkey and p_brand = 'Brand#12' and p_container in ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG') and lquantity >= 1 and lquantity <= 1 + 10 and p_size between 1 and 5 and l_shipmode in ('AIR', 'AIR REG') and l_shipinstruct = 'DELIVER IN PERSON' ) or ( ppartkey = lpartkey and p_brand = 'Brand#23' and p_container in ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK') and lquantity >= 10 and lquantity <= 10 + 10 and p_size between 1 and 10 and l_shipmode in ('AIR', 'AIR REG') and l_shipinstruct = 'DELIVER IN PERSON' ) or ( ppartkey = lpartkey and p_brand = 'Brand#34' and p_container in ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG') and lquantity >= 20 and lquantity <= 20 + 10 and p_size between 1 and 15 and l_shipmode in ('AIR', 'AIR REG') and l_shipinstruct = 'DELIVER IN PERSON' ); --Q20 select sname, saddress from supplier, nation where s_suppkey in ( select ps_suppkey from partsupp where ps_partkey in ( select p_partkey from part where p_name like 'forest%' ) and ps_availqty > ( select 0.5 * sum(l_quantity) from lineitem where lpartkey = pspartkey and lsuppkey = pssuppkey and l_shipdate >= date '1994-01-01' and l_shipdate < date '1994-01-01' + interval '1' year ) ) and snationkey = nnationkey and n_name = 'CANADA' order by s_name; --Q21 select s_name, count(*) as numwait from supplier, lineitem l1, orders, nation where ssuppkey = l1.lsuppkey and oorderkey = l1.lorderkey and o_orderstatus = 'F' and l1.lreceiptdate > l1.lcommitdate and exists ( select * from lineitem l2 where l2.lorderkey = l1.lorderkey and l2.lsuppkey <> l1.lsuppkey ) and not exists ( select * from lineitem l3 where l3.lorderkey = l1.lorderkey and l3.lsuppkey <> l1.lsuppkey and l3.lreceiptdate > l3.lcommitdate ) and snationkey = nnationkey and n_name = 'SAUDI ARABIA' group by s_name order by numwait desc, s_name; --Q22 select cntrycode, count(*) as numcust, sum(c_acctbal) as totacctbal from ( select substr(c_phone, 1, 2) as cntrycode, c_acctbal from customer where substr(c_phone, 1, 2) in ('13', '31', '23', '29', '30', '18', '17') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('13', '31', '23', '29', '30', '18', '17') ) and not exists ( select from orders where ocustkey = ccustkey