Structured Query Language is a language for interacting with relational databases. Read the tag wiki's guidelines for requesting SQL reviews: 1) Provide context, 2) Include the schema, 3) If asking about performance, include indexes and the output of EXPLAIN SELECT.

learn more… | top users | synonyms

3
votes
1answer
19 views

Change Data Capture using HIVE

Could you please review my code and let me know if there is something wrong? I'm getting the expected results. I'm trying to do change data capture using hive. We already have an existing CDC in ...
2
votes
1answer
13 views

Sorting of nested categories inside a Common Table Expression

I have a PostgreSQL database which contains (among other things) the following table definition: ...
7
votes
1answer
65 views

Filter out date records which don't fall within or span a date range

I have records which have specific time spans. The need is to select records which adhere to the following three rules (read them as or conditions) concerning a ...
10
votes
2answers
78 views

SQL-Server Merge Statement with .NET DataTable

Background: I'm trying to synchronize order information between Oracle and SQL-Server databases and allow users to be able to track any changes between what has been replicated via an ASP.NET web ...
2
votes
2answers
33 views

Assign Specific Value If BitField Is True

My colleague created this SQL to handle certain user selections which are based on an on/off bit flag. The goal is to identify those individual bit flags with a specific integer which will be used in ...
0
votes
0answers
22 views

display subcategories of selected category in tabular form [closed]

I have a form that has two parameters amenities and attributes. The amenities parameter has a dropdown menu that whose value gets populated from a separate table. The second parameter is attribute ...
2
votes
1answer
40 views

CAPTCHA with images of objects

This CAPTCHA is not text but basically images of objects that the users has to identify between an array of options. My current code is kind-of slow because the query takes almost .7s to execute and ...
5
votes
1answer
112 views

What are the latest Odds?

The main problem the following query is solving, is the following: Each Offer has multiple Odds (...
3
votes
2answers
39 views

Extract last record inserted for each key identifier

I have a table with this schema ID, int primary key PathKey, string not null InsertDate, datetime not null Value, int not null In the table there are a ...
0
votes
1answer
47 views

Correct MySQL DB Query and processing

I ve got a table called EXP it looks like this The table contains REG_ID - (id of the registred users) TRA_TYPE - (type of transportation the user chose 1-export 2-import) PRO_ID - (product id ...
3
votes
2answers
74 views

Finding the runtime of a task by name

Can this query be improved? I iterate over a couple of thousand times, and if I can improve it, it should decrease the run time. Otherwise I might look into using a batch instead of doing one at a ...
1
vote
2answers
103 views

Weekly fee calculation

I have the following query. Its execution took 15 min and it's too much slow. Is there a way to optimize it? ...
7
votes
3answers
246 views

Normalizing Invoices

I need to move data from one database to another, and since I don't have SSIS I'm doing this ETL with T-SQL scripts. One of the source tables contains invoice details, and features a column that ...
-2
votes
3answers
63 views

More intuitive formatting for a SQL insert formed with PHP

I wish to know how to make this code (and similar) more intuitive: ...
3
votes
2answers
93 views

Role-based access control query

I use SQL Server, and though not pertinent here, Railo's CFML engine. I'm not great at SQL Joins, but I worked through this one and got the result set that I want. This particular SQL will only ever ...
4
votes
1answer
60 views

High school social network query for friends in common

This is sourced from the Stanford Coursera self study DB class SQL quizzes: Students at your hometown high school have decided to organize their social network using databases. So far, they have ...
4
votes
1answer
41 views

Saving and updating records

I have made two SQL statements as I was unable to figure out how to solve my problem. One is called Product_GetID and the other ...
2
votes
0answers
44 views

Merging totals from rows with duplicate IDs

I have the following query which updates the quantity when there are duplicate items and users (there should only ever be 2 duplicates), then deletes the row which doesn't get updated... ...
0
votes
2answers
159 views

Optimize query containing too many inner join and rows

I have the following query: ...
6
votes
2answers
149 views

Follow up to Top Active Answerers on Stack Exchange site

This is a follow up to Top Active Answerers on a Stack Exchange site Actual SEDE Query --> Top Active Answerers on site Do I have a good understanding of the ...
3
votes
2answers
263 views

Top Active Answerers on a Stack Exchange site

I created this query back in January to find the top active answerers on Code Review and thought that it might be a good idea to get other opinions on my SQL coding using a database that we can all ...
2
votes
1answer
33 views

Query of attributes of mobile phone models, using several UNIONs to find distinct values

I'd like to know if there's any room to improve the following query which is to find distinct values from multiple tables when someone searches for a model name. Two of the tables are storing phones' ...
3
votes
1answer
26 views

SQL update statement

I have a SQL update statement which does what it's supposed to. However, I have a feeling that the way I wrote it isn't the best. I am not highly experienced in SQL so any pointers or hints on how can ...
7
votes
2answers
122 views

SQL service broker and threading

Basically my code waits for database table a to have an XML string inserted into it. The SQL query parses the XML string and stores it into separate columns (this ...
1
vote
1answer
21 views

MySQL proliferation of subqueries and left joins

I have the following query: ...
2
votes
3answers
212 views

Rewriting SQL IN statement with long list of parameters

These IDs seem like they should be a table as, there are several queries that use the same list of IDs. The following needs to be refactored. ...
6
votes
3answers
99 views

Step 2: Creating functions the business tool will use

I've been working on this project and here is my first function for step 2. I feel this is the most important one, as I will carry over recommendations from this ...
1
vote
1answer
27 views

Weighting mysql full-text search results by date

I'm using innodb full-text search to return a set of results where the newest posts are more relevant. Just wanted to check that this the best method: ...
6
votes
1answer
74 views

Database of students in a social network

This is sourced from the Stanford Coursera self study DB class SQL quizzes. Students at your hometown high school have decided to organize their social network using databases. So far, they have ...
5
votes
2answers
82 views

Average of averages

I need to get the average of multiple averages calculated basing on data contained on a sql database. To do that, I'm using this code: ...
1
vote
2answers
92 views

Chat server in Python

I just need to know if it's decent or not. Project home ...
5
votes
2answers
34 views

SQL Group and Filter - Refining down a search including Dates

The below process is designed to pick out only AsbestosUPRNs and get the lowest OverallRiskCategory where they have the newest SurveyDate. I just wanted to ensure this is the best way, it seems neat ...
6
votes
1answer
41 views

Query for finding possible “Not An Answer” posts

I have this query in SEDE to try to find possible not an answers to flag. (I already went through a lot of them, for Stack Overflow, so I don't really recommend anyone else using this one since isn't ...
4
votes
2answers
142 views

IMDB website query to find actors by time period

I'm using data from a database from the IMDB website. The database consists of five relevant tables. Actor (id, fname, lname, gender) Movie (id, name, year, rank) Director (id, fname, lname) Cast ...
4
votes
1answer
145 views

MySQL IMDB Query Speed

I'm using data from a database from the IMDB website. The database consists of five relevant tables. Actor (id, fname, lname, gender) Movie (id, name, year, rank) Director (id, fname, lname) Cast ...
2
votes
2answers
156 views

Does this column need to be indexed?

I have the following query which brings back a list of items which fall into a particular category: ...
3
votes
3answers
197 views

SQL delivery query performance

I need a "little" help with an SQL query (MySQL). I have the following tables: COURIERS table: ...
7
votes
2answers
91 views

Query for your Answer Progress

I was inspired to write a query that produced a graph mapping the perceived quality of your Stack Exchange answers over time.... in other words, "Are your answers getting better, or worse?" Because ...
5
votes
2answers
64 views

Cron job that runs a few checks across different tables in database

I have been working on a cronjob that runs a few checks across different tables in my database. The issue is that although it works, I feel like I am doing something horribly wrong in terms of the ...
3
votes
1answer
34 views

Basic T-SQL to compare two same length strings

I've come up with the following T-SQL to compare similar same length strings. Example usage would be: OCR returns a value which is expected to be in the database. PATINDEX is used to check the ...
3
votes
1answer
66 views

Query for combining two SELECTs to get active users and 1 inactive user

What this query does: Gets multiple users with the first select in the union; those are the active users. But when a user is ...
6
votes
3answers
195 views

Converting multiple query to use parameters to avoid SQL injection

I have some dropdownlist in my aspx page and I am using the choices from them in my SQL query: ...
2
votes
1answer
22 views

Optimizing sales report stored procedure

I need to optimize the following stored proc. Please let me know of any techniques or modifications that I can make to optimize this piece of code. The procedure is for a report that needs to run ...
0
votes
2answers
36 views

XSS Vulnerability on website

I'm using these functions to filter my all input variables. ...
1
vote
2answers
72 views

Does this code avoid SQL injection?

I have the following PHP code but I'm unsure, based on the many things I've read, whether or not this is actually safe from an SQL injection attack. ...
1
vote
1answer
31 views

Too many parentheses to format a percentage in SELECT

This query seems to have way too many parentheses. ...
3
votes
1answer
54 views

Monthly change query with SQL Server 2012

I'm new to SQL and working on a monthly change calculation. I want to calculate the change between this months balance and the previous months balance. This query is working for me but does not ...
3
votes
2answers
99 views

Similar statements need duplication removed

How would you refactor this? ExpensiveQuery must only run when necessary. ...
2
votes
2answers
76 views

Optimizing nested “Select Distinct” query

I am using the following query which is causing issues. Note following points: I am using 4 tables in this query: message_share ...
4
votes
3answers
54 views

Retrieve Father and Child structure in a Database Table

I have a table with this schema: CREATE TABLE [td].[MyTable] ( [ID] [int] NOT NULL ,[FatherID] [int] NULL ) (Note: I have excluded all the columns not ...