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

0
votes
2answers
102 views
6
votes
2answers
138 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
255 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
20 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
18 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
87 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
17 views

MySQL proliferation of subqueries and left joins

I have the following query: ...
2
votes
3answers
188 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
2answers
85 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
16 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
57 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
79 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
87 views

Chat server in Python

I just need to know if it's decent or not. Project home ...
5
votes
2answers
32 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
38 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
132 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
115 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
153 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
191 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
90 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
58 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
33 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
53 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
178 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
20 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
24 views

XSS Vulnerability on website

I'm using these functions to filter my all input variables. ...
1
vote
2answers
69 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
27 views

Too many parentheses to format a percentage in SELECT

This query seems to have way too many parentheses. ...
3
votes
1answer
39 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
97 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
49 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 ...
8
votes
2answers
58 views

Benchmarking of SQL data crunching using a common datum

@nhgrif and I were curious about a couple different ways to aggregate a number of records based on a duplicate/common datum. Two different ideas will be shown. Any advice on all code but especially ...
-2
votes
1answer
55 views

Loading data from SQL server to webpage takes too long

There's a div in my webpage which will load data from an SQL server when the user clicks the button loadresource. Here's the JavaScript for button click: ...
4
votes
1answer
67 views

Unduplicated counts over various dimensions

I'm working with some data on college students, which has a unique key of SSN, Campus, ...
5
votes
3answers
251 views

Can this SQL call for a scalar be made shorter?

When I work with SqlDataAdapter, I usually follow this pattern. Especially, please note the using/fill combination. It's basically a one-liner. The other stuff is just the facility around it. ...
3
votes
3answers
262 views

Get and display employee time clock data

I have been making a web page in PHP and JavaScript to take employees clock times and do some things with them so I can display them. Everything is currently working quite well, but the code looks ...
6
votes
5answers
356 views

Transaction handling for multiple SQL statements

In this code I update two tables called Payment and SalaryTrans. First I insert records (Salary payments) to ...
5
votes
3answers
190 views

Legacy Data and Current System Data Merged together in a single SQL Database

There was a lot going on in this Database that I couldn't change, a lot of bad practices because the data was originally being pulled from an old mainframe database and then transformed by a horrible ...
4
votes
2answers
488 views

Querying school databases

This is the application I created for my personal use. I need some quality testing of this application. If you see some errors or any lengthy coding (it's already lengthy), and it needs to be ...
5
votes
1answer
46 views

SQL query with dynamic unpivot+pivot for cross product

In this Data Explorer query I am trying to do the following: For each tag: Compute sum of answer scores in this tag ("S") Compute count of answers in this tag ("A") For each tag class (Bronze, ...
4
votes
3answers
101 views

SQL design for representing a two-level hierarchy of objects with containment

I am trying to represent questions in quizzes. The database is Mysql but I am using fairly generic sql. The assumption is that varchar cannot handle more than 255 though, so I am supporting longer ...
5
votes
2answers
189 views

General select statement to stored procedures

I'm trying to write a general function to several specific stored procedures, but I'm afraid that it will cause errors that I don't see now. The function is to get the name of the stored procedure as ...
3
votes
2answers
122 views

Efficient way to extract the first row in a Group By group

I have a large Sql Server view with this schema: [archive_ID] (int, not null) [archive_date] (datetime, not null) [archdata_path_ID] (varchar(50), not null) [archdata_value] (int not null) I need ...
5
votes
3answers
79 views

Relational schema and query for multiplayer tabletop game

As an exercise, I was asked to design a database schema (for MS SQL Server) for a tabletop game. The requirements were simple: players compete in matches and there are specific match types (e.g. 1v1 ...
19
votes
6answers
2k views

Average time to first answer

Just trying out my T-SQL foo. Wrote a script to get the average time (in seconds) to first answer for a specified language. ...
5
votes
3answers
238 views

Extracting certain records

The purpose of this SQL query would be to extract the record 10192 only, because the end_time is both in the past and its foreign counterpart in the Node table ...
2
votes
1answer
32 views

Reusing select statement

I am checking if there are any rows in a table with some conditions. If there are more than 1 row, it should filter again with some more conditions. ...
6
votes
1answer
40 views

Trying to avoid a double full scan with aggregates in query

I have a statistics query I'm trying to run against the below table: ...
5
votes
1answer
77 views

Selecting template from database using organization ids

This is my first SQL post over here, and in fact, I have more or less the experience of a regular SQL programmer, except that the task at hand this time was way harder than normal. I am expecting, ...